一个简单的基于sqlite3的python对象存储
minidb的Python项目详细描述
_|.\uuu/
简单的python对象存储
tutorial
--
让我们从导入python中的minidb模块开始。3:
>;>;导入minidb
要在内存中创建存储,我们只需实例化minidb.store,或者告诉它将SQL语句输出为调试输出:
>;>;db=minidb.store(debug=true)
创建minidb.store时,只需传入一个文件名作为第一个
参数:
>;>db=minidb.store('filename.db',debug=true)
类可持久化,并且
添加使用数据库的方法:
>;>class person(minidb.model):
…名称=str
…电子邮件=str
…age=int
minidb.model的每个子类也将有一个"id"属性,如果数据库中没有存储实例,则该属性为none;如果数据库中有实例,则该属性为automatically assigne值。这唯一地标识了数据库中的对象。
现在是向存储注册minidb.model子类的时候了:
>;>db.register(person)
并创建必要的结构(只有在将debug=true传递给minidb.store的构造函数时,才会显示此
输出):
:pragma table_info(person)
:创建表person(id integer primary key,
名称文本,
电子邮件文本,
年龄整数)
现在您可以创建minidb.model子类的实例,可以选择传递用于初始化字段的关键字参数:
>;>;p=person(name='hello world',email='minidb@example.com',age=99)
>;>;p
<;person(id=none,name='hello world',email='minidb@example.com',age=99)>;
我们将看到它如何在数据库中存储对象:
:插入person(姓名、电子邮件、年龄)值(?),??)
['hello world','minidb@example.com','99']
或者
从中加载它的minidb.store对象,以便下次保存该对象时可以省去
>;p.name='再次问候'
>;p.save()
,商店会找出需要做什么:
:更新人员集名称=?,电子邮件=?年龄=?ID在哪里?
['hello again','minidb@example.com','99',1]
现在,让我们插入更多数据,只是为了好玩:
>;>;对于范围(10)内的i:
…person(name='hello',email='x@example.org',age=10+i*3).save(db)
print(person,person is p)
person.load()执行的SQL查询是:
:选择ID、姓名、电子邮件、年龄,从person
[]
人(id=2,name='hello',email='x@example.org,年龄=10)>;假
<;人(id=3,name='hello',email='x@example.org',年龄=13)>;假
<;人(id=4,name='hello',email='x@example.org',年龄=16)>;假
<;人(id=5,name='hello',email='x@example.org',年龄=19)>;假
<;人(id=6,name='hello',email='x@example.org',age=22)>;假
<;人(id=7,name='hello',email='x@example.org',age=25)>;假
<;人(id=8,name='hello',email='x@example.org',age=28)>;假
<;人(id=9,name='hello',email='x@example.org',age=31)>;假
<;人(id=10,name='hello',email='x@example.org',age=34)>;false
<;person(id=11,name='hello',email='x@example.org',age=37)>;false
minidb缓存对象,只要您有对它们的
引用,就可以取而代之地检索这些对象。
这确保所有对象保持同步,让我们尝试修改person.get()返回的对象
,该函数只检索一个对象:
>;>;>print(p.name)
再次问候
>;>person.get(db,id=1)。name='hello'
>;>print(p.name)
hello
minidb.model子类有一个名为"c"的类
属性,可用于引用列/属性:
>;person.c
<;person的列(姓名、电子邮件、年龄)>;
>;>person.load(db,(person.c.age>;=16)&;(person.c.age<;=50))
这将运行以下SQL查询:
:选择id、name、email、age from person where(age>;=?)和(age<;=?)
[16,50]
您也可以查询列,而不是查询完整的对象,例如,我们可以在表中找到最小和最大年龄值:
>;>;next(person.query(db,person.c.age.min//person.c.age.max))
(10,99)
查询如下:
:从person
[]
注意column1//column2是
minidb.columns(column1,column2)更详细语法的语法糖。query()方法返回一个
行的生成器,您可以通过python内置的next()获得一行。每一行可以用不同的方式访问:
1。作为元组(这也是打印行时的默认表示)
2。作为字典
3。作为具有属性的对象,例如,作为字典:
>;>dict(next(person.query(db,person.c.age.min))
{min(age):10}
person.c.age.min('最小年龄'))
{最小年龄:10}
下一个(person.query(db,person.c.age.min('minimum戋age'))。minimum戋age
10
按=person.c.age.desc,limit=5)排序(按=person.c.age.age.desc,limit=5))
>若要保存键入,您可以执行以下操作:
>
>>>>gt;person.c.name//person.c.电子邮件。查询(db)查询(db)查询(db,按=lamdba c:c.age.age.desc,按=lamdba c:c.age.age.desc排序)查询(db,按=lamdba c:c.age.age.desc排序)查询(db,按=lamdba c:c.age.age.age.desc,排序)
>>
>>>>>>(一)有权使用;>;person.query(db,lambda c:c.name//c.email)