Database-based stores¶
SQLAlchemy¶
To store data in existing databases, an SQLAlchemy based store is available:
from simplekv.db.sql import SQLAlchemyStore
from sqlalchemy import create_engine, MetaData
# use echo=True to see queries
engine = create_engine('sqlite:///:memory:', echo=True)
metadata = MetaData(bind=engine)
# init store
store = SQLAlchemyStore(engine, metadata, 'kvstore')
# create the actual table in the database (only do this once!)
metadata.create_all()
# also possible: store.table.create()
# use store normally
store.put('my_key', 'some value')
print store.get('my_key')
-
class
simplekv.db.sql.
SQLAlchemyStore
¶ Stores data in a table in a database through SQLAlchemy.
Note that this storage is not well-suited for large binary data, as currently it does not support streaming of large blobs. In other words, every value must be read into memory, before it can be returned.
-
__init__
(bind, metadata, tablename)¶ Generates a new
Table
for use as a backend (seetable
) on the supplied metadata.Parameters: - bind – Any queries made by the store run
execute()
using this bind. - metadata –
sqlalchemy.schema.MetaData
instance on which the table will be created. - tablename – The name for the table.
- bind – Any queries made by the store run
-
table
¶ An
sqlalchemy.schema.Table
instance autogenerated by__init__()
. Callingcreate()
can be used to create the table in the database.
-
MongoDB¶
The MongoStore
class requires the pymongo
package to be installed.