You don‘t have to define the field types. PyDbLite will accept any value that can be serialized by the
cPickle module : strings, Unicode strings, integers, floats, dates and datetimes (instances of the date and datetime classes in the datetime module), user-defined classes, etccreate() method, to specify what you want to do if the base already exists in the file system
- mode = "open" : db.create(‘name‘,‘age‘,‘size‘,mode="open") opens the database and ignores the field definition
- mode = "override" : db.create(‘name‘,‘age‘,‘size‘,mode="override") erases the existing base and creates a new one with the field definition
- if mode is not specified and the base already exists, an
IOErroris raised
- by keywords : db.insert(name=‘homer‘,age=23,size=1.84)
If some fields are missing, they are initialized with the valueNone - by positional arguments : db.insert(‘homer‘,23,1.84)
The arguments must be provided in the same order as in thecreate()method
If you don‘t commit the changes, the insertion, deletion and update operations will not be saved on disk. To return to the previous version, just open() it again (this is equivalent to rollback in transactional databases)
create() method, an internal field called __id__ is added. It is a integer which is guaranteed to be unique and unchanged for each record in the base, so that it can be used as the record identifier__version__ is also managed by the database engine. It is a integer which is set to 0 when the record is created, then incremented by 1 each time the record is updated. This is used to detect concurrency control, for instance in a web application where 2 users select the same record and want to update it at the same timerecs = [ r for r in db if 30 > r[‘age‘] >= 18 and r[‘size‘] < 2 ]
returns the records in the base where the age is between 18 and 30, and size is below 2 meters. The record is a dictionary, where the key is the field name and value is the field value
for r in (r for r in db if r[‘name‘] in (‘homer‘,‘marge‘) ):
do_something_with(r)
iterates on the records where the name is one of ‘homer‘ or ‘marge‘
for r in db:
do_something_with(r)
When an index is created, the database instance has an attribute (here
_age : note the heading underscore, to avoid name conflicts with internal names). This attribute is a dictionary-like object, where keys are the values taken by the field, and values are the records whose field values are egal to the key : records = db._age[23] returns the list of records with age == 23
If no record has this value, lookup by this value returns an empty list
The index supports iteration on the field values, and the
keys() method returns all existing values for the fieldlist_of_records can be any iterable (list, tuple, set, etc) yielding recordsNoneimport pydblite # 使用内存数据库 pydb = pydblite.Base("address") # 创建a,b,c三个字段 pydb.create(‘a‘, ‘b‘, ‘c‘) # 为字段a,b创建索引 pydb.create_index(‘a‘, ‘b‘) # 插入一条数据 pydb.insert(a=0, b=0, c=1) pydb.insert(a=1, b=0, c=1) pydb.insert(a=1, b=0, c=1) pydb.insert(a=1, b=0, c=1) pydb.update(records=pydb[1],a=2,c="li") pydb.delete(pydb[0]) # 查询符合特定要求的数据 results = pydb(a=2) for i in results: print results[i]
python内存数据库pydblite
标签: