Skip to content

SQLite

SQLite keeps a graph in a file, with no server: drivine4j’s store, in the same tables as Postgres, so a file one library wrote, drivine4j, Drivine for TypeScript or this one, the others read and write as it is. pip install "drivine[sqlite]" installs what it takes: apsw, whose wheels bundle a SQLite of their own (3.53.2 or later). So the store runs on the same SQLite whatever SQLite Python was built with, as drivine4j’s and Drivine for TypeScript’s run on the one their drivers bundle. Models, the query DSL and the object manager are the same as on the other stores; the persistence manager runs plain SQL, with cursors and transactions, and Cypher you write is compiled to SQLite’s SQL, as on Postgres and Oracle: see Cypher on Postgres, Oracle and SQLite. SQLite 3.53.2 writes a float as text to 17 significant digits (3.53.0 and 3.53.1 still write 15), so a float compiled Cypher gives is the one a graph engine gives, and it runs a CALL { … } that groups by a variable it imports. A SQLite older than 3.53.2, as an apsw built against an older SQLite of the system runs, is refused as a connection opens.

Breaking change. SQLite now goes through apsw and the SQLite it bundles: pip install "drivine[sqlite]" brings it, and pip install drivine alone no longer opens a SQLite store. Python’s own sqlite3 module and pysqlite3 are no longer used, and Drivine no longer warns that floats may lose their last digits, as no SQLite it runs on does. A failure inside a function Drivine registers, such as a regular expression that doesn’t compile, is raised as itself, and SQLite’s own failures are apsw’s (apsw.Error, apsw.ConstraintError, apsw.BusyError …), not sqlite3.Error. A named parameter given no value, or a text of more than one statement, is refused with DrivineError, not sqlite3.ProgrammingError. The SQLite file is the same: one written before reads and writes as it did (#376).

DATABASE_TYPE=SQLITE
DATABASE_NAME=/var/lib/app/graph.db # the file's path: there is no host, port, user or password
DATABASE_DEFAULT_GRAPH_PATH=drivine # names the graph's tables; this is the default

Or in code, registry.builder().database_type(DatabaseType.SQLITE).database_name("graph.db").register("graph"). The file is made if it isn’t there, and the graph’s tables on the first connection, as on Postgres. :memory: is refused: each pooled connection would open a database of its own. Each connection’s calls run on a thread of the connection’s own, in both APIs, started with a 64 MB stack, so a regular expression is answered as deep whichever thread asks; in the async API one waiting for the file’s lock holds up neither the event loop nor another connection.

How a graph is stored. <graph>_node (nid INTEGER PRIMARY KEY AUTOINCREMENT, labels TEXT, props TEXT, stamp TEXT, words TEXT), <graph>_edge (eid INTEGER PRIMARY KEY AUTOINCREMENT, src, dst REFERENCES <graph>_node ON DELETE CASCADE, type, props TEXT) and <graph>_schema, the indexes and constraints Drivine made, with the shared module’s tables of counts and its lock (<graph>_count2, <graph>_count2_delta, <graph>_lock2). Labels and properties are JSON text, not SQLite’s binary jsonb, and labels are written sorted, with nothing escaped that JSON does not require: a label is found by its text. The stamp is a column. In the query DSL a moment compares and orders as Cypher’s does: by its instant, and equal only to one at the same offset.

The id of a node or relationship that is deleted is never given to another, within a statement, a transaction or after it, as on Oracle and Postgres: an element id an application holds names the element it was read from or nothing. SQLite keeps the greatest id each table has given in its own table, sqlite_sequence; deleting every row, as a test’s cleanup does, leaves it, so ids go on from there. A file whose graph tables an earlier Drivine made, with INTEGER PRIMARY KEY alone, which gives a new row the id of the last one deleted, is refused on the provider’s first connection and on every one after it, with a ReusedIdsError, from drivine and drivine.sync, that names the graph and the file. Its graph is the graph’s name, as the provider was given it, and its path the file, None for a database in memory or in a temporary file. There is no migration: make a new file and save the graph into it again.

  • A save is the plan of steps Postgres’s write function carries out, carried out by a function Drivine registers on each connection, drivine_write, under a savepoint: applied whole or not at all, in a transaction or not. The other functions a statement calls are registered too: drivine_delete, drivine_lower (Unicode’s lower case, where SQLite’s lowers ASCII), drivine_similarity and drivine_regexp_count, and the shared module’s, loaded from its SQLite extension in the drivine-cypher-compiler wheel, REGEXP among them. A statement of yours may call them; a connection you open yourself has none of them.
  • A statement Drivine writes that gives back documents opens with /* drivine:json */, and its text values are read as JSON. One you write is read as it is: a row is a dict of its columns, and labels and props are text.
  • A raw where is SQL over the root’s row n, as on Postgres, read over the row a statement calls it by, n_ (the row the shared module’s fragments read); a property written as Cypher writes it, n.name, is read as that property, as text, and one whose stored name begins with $, n.`$score` , by the name the store holds it by; one compared with true or false, or with a parameter bound to True or False, is compared with the text a JSON boolean is read as, while SQL of your own over n.props reads one as SQLite does, 1 or 0, as the parameter is bound. To compare one with a number, read it as a number in SQL: CAST(n.`$score` AS NUMERIC) > $least. A date in a raw where is compared with the property’s JSON, the tag a save holds it as, so n.at = $when finds nothing: compare dates in the query DSL.
  • A range index or a uniqueness constraint is a partial index over the property as a statement reads it, for the label’s nodes. A vector or a full-text index is recorded and nothing is made for it: load_nearest and load_matching measure every node of the label, and load_matching scores by BM25, as Postgres does.
  • matches takes a regular expression in Java’s dialect, as on drivine4j, matched against the whole value: a REGEXP, the shared module’s, reads it as Java does, and refuses what it can’t read so, such as a look-behind.

What a transaction gives. Each connection writes ahead to a log, enforces foreign keys and waits for a locked file for up to its busy timeout, 30 seconds by default.

  • There is one writer at a time. A transaction takes the file’s write lock as it begins (BEGIN IMMEDIATE), so even a transaction that only reads waits for the lock, for up to the busy timeout. One that holds it longer than 5 seconds is logged as a warning when it ends, committed, rolled back or released.
  • Reads outside a transaction don’t wait for a writer: each reads the file as it last was.
  • Saves, patches and deletes outside a transaction are run again when SQLite turns them away as contended (SQLITE_BUSY).
  • Each statement Drivine writes applies whole or not at all.
  • A connection you open yourself, with sqlite3.connect, has none of this: no foreign keys, no write lock taken as a transaction begins, no warning, Python’s wait of 5 seconds for a locked file rather than 30, and none of Drivine’s functions. It does write ahead to the log, as the file keeps that mode once Drivine has set it.

Drivine reads os.environ, and doesn’t load .env files itself: use python-dotenv, pydantic-settings, or your platform’s way.