I would like to store Python objects into a SQLite database. Is that possible?
If so what would be some links / examples for it?
You other choice instead of pickling is to use an ORM. This lets you map rows in a database to an object. See http://wiki.python.org/moin/HigherLevelDatabaseProgramming for a starting point. I'd recommend SQLAlchemy or SQLObject.
One option is to use an O/R mapper like SQLObject. It will do most of the plumbing to persist the Python object to a database, and it supports SQLite. As mentioned elsewhere you can also serialise the object using a method such as pickle, which dumps out a representation of the object that it can reconstruct by reading back in and parsing.
As others have mentioned, the answer is yes... but the object needs to be serialized first. I'm the author of a package called klepto
that is built to seamlessly store python objects in SQL databases, HDF archives, and other types of key-value stores.
It provides a simple dictionary interface, like this:
>>> from klepto.archives import sqltable_archive as sql_archive
>>> d = sql_archive(cached=False)
>>> d['a'] = 1
>>> d['b'] = '1'
>>> d['c'] = min
>>> squared = lambda x:x*x
>>> d['d'] = squared
>>> class Foo(object):
... def __init__(self, x):
... self.x = x
... def __call__(self):
... return squared(self.x)
...
>>> f = Foo(2)
>>> d['e'] = Foo
>>> d['f'] = f
>>>
>>> d
sqltable_archive('sqlite:///:memory:?table=memo' {'a': 1, 'b': '1', 'c': <built-in function min>, 'd': <function <lambda> at 0x10f631268>, 'e': <class '__main__.Foo'>, 'f': <__main__.Foo object at 0x10f63d908>}, cached=False)
>>>
>>> # min(squared(2), 1)
>>> d['c'](d['f'](), d['a'])
1
>>>
The cached
keyword in the archive constructor signifies whether you want to use a local memory cache, with the archive set as the cache backend (cached=True
) or just use the archive directly (cached=False
). Under the covers, it can use pickle
, json
, dill
, or other serializers to pickle the objects. Looking at the archive's internals, you can see it's leveraging SQLAlchemy
:
>>> d._engine
Engine(sqlite://)
>>> d.__state__
{'serialized': True, 'root': 'sqlite:///:memory:', 'id': Table('memo', MetaData(bind=None), Column('Kkey', String(length=255), table=<memo>, primary_key=True, nullable=False), Column('Kval', PickleType(), table=<memo>), schema=None), 'protocol': 3, 'config': {}}