cysqlite documentation

_images/logo.png

cysqlite provides performant bindings to SQLite. cysqlite aims to be roughly compatible with the behavior of the standard lib sqlite3 module.

cysqlite supports standalone builds or dynamic-linking with the system SQLite.

cysqlite is a Cython-based SQLite driver that provides:

  • DB-API 2.0 compatible

  • High-performance query execution

  • Transaction management with context-managers and decorators

  • User-defined functions, aggregates, window functions, and virtual tables

  • BLOB support

  • Row objects with dict-like access

  • Schema introspection utilities

  • Asyncio support

  • Easy to create fully self-contained builds

Note

If you are looking for a SQLite driver that “just works” wherever your application will be used or installed (e.g. you are a library developer), the standard lib sqlite3 is the best choice.

If you are looking for a SQLite driver which exposes the full surface-area of SQLite APIs, apsw is the best choice.

Note

cysqlite is well-supported by peewee ORM.

Example usage:

from cysqlite import connect

db = connect(':memory:')

db.execute('create table data (k, v)')

with db.atomic():
    db.executemany('insert into data (k, v) values (?, ?)',
                   [(f'k{i:02d}', f'v{i:02d}') for i in range(10)])
    print(db.last_insert_rowid())  # 10.

curs = db.execute('select * from data')
for row in curs:
    print(row)  # e.g., ('k00', 'v00')

# We can use named parameters with a dict as well.
row = db.execute_one('select * from data where k = :key and v = :val',
                     {'key': 'k05', 'val': 'v05'})
print(row)  # ('k05', 'v05')

db.close()