Sunday, March 16, 2008

Python Database framework - PyDbLite

PyDbLite - a small & fast in-memory database engine - PyDbLite.pyAuthor: Pierre Quentel

While preparing the AJAX project and looking for database solutions, PyDbLite
is one of the possible optional. PyDbLite is a pur-Python in-memory database
engine, use list as query language. It is very simple and easy to implement,
but it will take up memory to hold the database, so it is not suitable for a
large scale database application.

It comes with a Base class and allow us to to create, open, update, delete, query the
database and create index for the database file we need. While creating the database, we do not have to defind data type. PyDbLite will accept any value that can be serialized by the cPickle module.

>> code samples <<
Complete reference

from PyDbLite import Base
db = Base('dummy')
db.create('name','age','size',mode="open" )
db.open()
db.insert(name='homer',age=23,size=1.84)
db.commit()
recs = [ r for r in db if 30 > r['age'] >= 18 and r['size'] < 2 ]

for r in (r for r in db if r['name'] in ('homer','marge') ):
do_something_with(r)

for r in db:
do_something_with(r)

db.create_index('age')
records = db._age[23]
len(db)
db.delete(record)
db.update(record,age=24)
db.add_field('new_field'[,default=v])
db.drop_field('name')
db.fields

No comments: