Is there a lightweight database wrapper in Python that I can use for SQLite. I would like something like Django\'s ORM, but that I can just point to a database file and it\'ll m
Disclaimer : I have an affiliation with package, sql30, suggested here.
sql30 can be another potential option for lightweight CRUD operations on SQLITE
database. It is a zero weight ORM, written using only native python constructs and has no dependency on any other module which makes it great option for platforms which have limited or lagging support of Python such as ESX .
For the use case, asked in question here, user can simply
Create a Class object referring to database file
Define table schema (based on the columns of table) using simple JSON.
and you are ready for any CRUD operations.
An example is shown below.
Say the database file is reviews.db
and one of the table in it is reviews
which has fields of individual reviews such as rid, header, rating and desc(iption)
. One can do the CRUD operations as below.
# reviews.py
from sql30 import db
class Reviews(db.Model):
PKEY = 'rid'
DB_SCHEMA = {
'db_name': 'reviews.db',
'tables': [
{
'name': 'reviews',
'fields': {
'rid': 'uuid',
'header': 'text',
'rating': 'int',
'desc': 'text'
},
'primary_key': 'rid'
}]
}
VALIDATE_BEFORE_WRITE = True
One can do CRUD operations as below now.
>>> import os
>>> import reviews
# Create ORM layer object instance.
>>> db = reviews.Reviews()
# With this, we can create/write records to db as following.
>>> tbl = 'reviews' # select database table, you want to operate on.
>>> db.write(tbl, rid=1, header='good thing', rating=5)
>>> db.write(tbl, rid=2, header='bad thing', rating=1, desc='what a disgusting thing')
# We can then read back the records individually are as whole as shown below.
# To read all the records from a table, simply pass the table name.
>>> db.read(tbl)
[(1, 'good thing', 5, ''), (2, 'bad thing', 1, 'what a disgusting thing')]
# To read the records from table, simply pass on the condition as params.
>>> db.read(tbl, rid=1) # Get records from table WHERE rid=1
[(1, 'good thing', 5, '')]
# Get records from table WHERE rid=1 and rating=3. Note that, there is no
# restriction on the order in which condition needs to be provided. Only
# the param name should be one of the COLUMN(s) in table.
>>> db.read(tbl, rating=3, rid=1)
[]
>>> db.read(tbl, rating=5, rid=1)
[(1, 'good thing', 5, '')]
# If we try to add another record with same primary key, it will error out.
>>> db.write(tbl, rid=1, header='good thing', rating=5)
Traceback (most recent call last):
...
...
sqlite3.IntegrityError: UNIQUE constraint failed: reviews.rid
# Updating the record is also possible by providing condition for records and updated values.
>>> where = {'rid': 2}
>>> db.update(tbl, condition=where, header='average item', rating=2)
>>> db.read(tbl)
[(1, 'good thing', 5, ''), (2, 'average item', 2, 'what a disgusting thing')]
# Deleteing the records is possble with any level of filtering.
>>> db.remove(tbl, rid=1)
>>> db.read(tbl)
[(2, 'average item', 2, 'what a disgusting thing')]
# At this point, however, all of your records are being maintained by SQLITE in-memory.
# However, commit API can be used to persist them in the DB file.
>>> db.commit()
Parallel operations are allowed and handled well as long as DB connection is used within same thread, which is essentially sqlite
module requirement. Example of this is here.