minidb - A simple SQLite3 store for Python objects

minidb is a Python module that utilizes the SQLite3 database library in order to store and retrieve Python objects. It utilizes Python's __slots__ mechanism to determine the column names, and uses the class name for table names. Data is always stored as text in the database, but will be converted using the type specified in __slots__ (which therefore has to be a dict).

This is useful for small projects that just require some kind of persistence layer, but where you might want to process the stored data with other tools/languages. It's not suited for databases with more than a few hundred entries, but works fine for small datasets.

You can use list comprehension and generator expressions to build more sophisticated queries (minidb supports only equality):

l = [p for p in db.load(Person) if p.age > 18 and 'Jen' in p.name]

(Yes, this requires all objects to be loaded from the database and converted into Python objects, but this overhead is paid for by comfort and easy readability. Errors here also happen in Python land and should be easier to debug than errors in equivalent SQL queries that happen in the database engine.)

News

2012-11-28: minidb 1.1 released

Version 1.1 of minidb is out. This release fixes a small issue with databases growing over time (see gPodder bug 1721). The new releases issues an SQLite VACUUM command when the database is closed. As the minidb module is only suited for small database sizes ("a few hundred entries"), the vacuuming should be fast enough.

Links

Example code

import minidb

db = minidb.Store('persons.db')

class Person(object):
    __slots__ = {'id': int, 'name': str}
    def __init__(self, id, name):
        self.id = id
        self.name = name

john = Person(42, 'John')
db.save(john)

for person in db.load(Person):
    print person.id, person.name

Output: 42 John

db.update(john, name='Bob')

print db.get(Person, id=42).name

Output: Bob

Download

License

Copyright 2009-2010 Thomas Perl <thp.io>. All rights reserved.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Git repository

The source code repository for minidb is hosted at repo.or.cz:

git clone git://repo.or.cz/minidb.git

Or view it via the web interface.

Projects using minidb

If you are using minidb for your project, please drop me a mail and let me know, so I can link your project here :)

Thomas Perl · 2012-11-28