Part 19 of 20 in Tools we built and why. Previous: mojo-amdgpu: reading your graphics card's temperature and memory · Next: How we test a tool before we share it
What this is about
A database is a place to keep information so you can ask questions about it later. Most databases run as a separate program, a server, and your program talks to it over a network. SQLite is different. The whole database is one file on your disk, and your program reads it directly through a library. There is no server to install or start.
That makes SQLite a good fit for small tools, apps, caches and records you want to keep next to your program. mojo-sqlite is our binding for it from Mojo. It returns the same rows as Python's built-in sqlite3 module, byte for byte.
Why it matters
Almost every program ends up needing to keep a list of things: jobs, results, settings, a log. A text file works until you need to find, sort or update one record without rewriting the rest. A database does that for you. And because SQLite is a file, you can copy it, back it up, or open it in another tool.
If your tool is written in Mojo and needs to keep records, you need a way in. That is what this library is.
How it works
SQLite's language is SQL. You make a table, put rows in it, and ask for rows back. The README's example does it in a few lines:
from sqlite import Database
def main() raises:
var db = Database(":memory:")
db.exec("CREATE TABLE jobs(cmd TEXT, prio INTEGER); INSERT INTO jobs VALUES ('train', 10), ('eval', 20)")
var q = db.prepare("SELECT cmd, prio FROM jobs WHERE prio >= ?")
q.bind(1, 15)
while q.step():
print(q.text(0), q.int(1))
Read it slowly. Database(":memory:") opens a database that lives in memory, so nothing is saved to disk. exec runs a script that makes a table and adds two rows. prepare compiles a question once. bind puts the number 15 in place of the question mark. step walks through the matching rows, here only eval, which has priority 20. A prepared statement can be bound and run many times, which is where the speed comes from.
The library loads the system's libsqlite3.so.0 at run time, so you need no linker flags. Errors carry SQLite's own message, such as UNIQUE constraint failed.
Two small features save copying. text_view and blob_view read directly from SQLite's own buffer.
How it is checked
There is a folder of SQL files in tests/cases/. Every file runs through this library and through Python's sqlite3, with the same system libsqlite3, and the outputs must be identical. That includes integers, the exact bits of every float, text with tabs and newlines, NULL and binary blobs. CI runs on Ubuntu 24.04 with SQLite 3.45, and the check also passes on SQLite 3.54.
How fast is it?
The benchmark does 200,000 inserts through one prepared statement inside one transaction, then a full scan, on a database file in WAL mode. Three runs each.
| insert | scan | peak memory | |
|---|---|---|---|
| Python sqlite3 | 216 to 226 ms | 98 to 99 ms | 14.5 MB |
mojo-sqlite with text() |
80 to 82 ms | 25 to 29 ms | 15.4 MB |
mojo-sqlite with text_view() |
79 to 82 ms | 18 to 23 ms | 15.2 MB |
That is 2.7 times faster on inserts and 4 to 5.5 times faster on scans. The memory is a little higher because the Mojo runtime itself takes 11.6 MB, even for an empty program. These numbers come from one machine. They show a trend, and they do not promise a ratio on your data. Both sides call the same SQLite C library, so the gap comes from the code around it.
How to do it, step by step
- Install with
pixi add mojo-sqliteafter adding the amarbaro channel. The import name issqlite. - Copy the example above into a file and run it.
- Change
:memory:to a file name such asjobs.db. Run it twice. The second run finds the rows from the first, because the data is saved in the file. - Open
jobs.dbwith thesqlite3command line and look at your table. - Use
beginandcommitaround many inserts. The benchmark above ran all its inserts inside one transaction, and the speed numbers assume that.
What it does not do
- One connection per
Database. Do not share aDatabaseorStatementacross threads. - It runs on Linux only, because it loads
libsqlite3.so.0. - It is a binding. It does not replace SQLite or change how SQLite stores data.
- It does not wrap every feature of the C library.
Words you may not know
- Database: an organized store of information you can query.
- SQL: the language for asking a database questions.
- Table and row: a table is a grid, and each row is one record.
- Prepared statement: a question compiled once and run many times.
- Transaction: a group of changes that succeed or fail together.
- WAL: write-ahead logging, a way SQLite saves changes safely and quickly.
- Binding: a library that lets one language use a library written in another.
- NULL: the value that means nothing is stored.
Try it
- Read the README at github.com/amarbaro/mojo-sqlite.
- See where it sits at the Mojo libraries page.
- Run the example, then save to a file and reopen it.
- Compare it with Python on your own data, and read the benchmark folder for how we measure.
Read next
- Why our Mojo libraries copy Python byte for byte: why identical rows are the bar.
- How we test a tool before we share it: the last post in this series.

Commentaires
Pas encore de commentaires.
Se connecter pour commenter.