Exercise: Writing a SQLite database from Rust
Goal
Build the strain table from scratch out of a TSV file: open an empty .db, CREATE TABLE, then bulk-insert every row inside one transaction.
fn create_schema(conn: &Connection) -> Result<()>;
fn parse_tsv<R: BufRead>(reader: R) -> std::io::Result<Vec<StrainInput>>;
fn insert_strains(conn: &mut Connection, strains: &[StrainInput]) -> Result<usize>;By the end you will have used Connection::execute_batch, Connection::transaction, a prepared INSERT inside a loop, the params! macro, and Option<T> parameter binding (for the nullable columns).
This is the part where SQL stops being “those queries the analyst types in the CLI” and becomes “the storage format our pipeline emits”. It is short, but it is the most-used pattern in the whole day.
The exercise
cd day7/ex-strain-writeThe starter contains:
Cargo.tomlwithrusqlite = { version = "0.31", features = ["bundled"] }.src/main.rswith the three function signatures, amainthat wires them together, and amod testsblock that exercises both the parser and the database layer.data/strains.tsv— the same 10 strains as the previous exercises, but as TSV with a header line. No pre-built.dbfile — you are about to build it.
Implement all three functions until five tests pass.
How to run
cargo test
cargo run -- data/strains.tsv data/strains.dbExpected output:
inserted 10 strains into data/strains.db
Verify what you wrote with the sqlite3 CLI you used yesterday:
sqlite3 data/strains.db 'SELECT species, COUNT(*) FROM strain GROUP BY species'Hints
create_schema is one method call
Connection::execute_batch runs a string that may contain several semicolon-separated SQL statements. Perfect for CREATE TABLE:
conn.execute_batch(
"CREATE TABLE strain (
strain_id INTEGER PRIMARY KEY,
strain_name TEXT NOT NULL,
species TEXT NOT NULL,
isolation_source TEXT,
year_isolated INTEGER
);"
)You can add the assay schema in the same call if you want — the solution does.
Same shape as the day-4 BED parser. Skip the header line, split each remaining line on '\t', and convert empty strings to None:
for (i, line) in reader.lines().enumerate() {
let line = line?;
if i == 0 || line.is_empty() { continue; }
let mut parts = line.split('\t');
let strain_name = parts.next().unwrap_or("").trim().to_string();
// ... three more fields, last two are Option ...
}For the year, parse the string into i64 and wrap the parse error into a std::io::Error::new. You do not need a fancy error type today.
Connection::transaction needs &mut Connection (which is why the function takes &mut). Prepare the INSERT once, reuse it in the loop, then drop the prepared statement before commit():
let tx = conn.transaction()?;
{
let mut stmt = tx.prepare(
"INSERT INTO strain (strain_name, species, isolation_source, year_isolated)
VALUES (?1, ?2, ?3, ?4)",
)?;
for s in strains {
stmt.execute(rusqlite::params![
s.strain_name,
s.species,
s.isolation_source,
s.year_isolated,
])?;
}
}
tx.commit()?;The braces around the prepare/loop are a Rust-only quirk: stmt borrows from tx, and tx.commit() consumes tx, so stmt has to be dropped first. The inner scope makes that obvious.
Without a transaction, every INSERT would fsync() to disk separately — about 1000× slower for a bulk load. Always wrap bulk writes.
Option<T> as a parameter
rusqlite knows how to bind None as SQL NULL and Some(x) as x for every primitive. So:
stmt.execute(params![
s.strain_name, // String -> TEXT
s.species, // String -> TEXT
s.isolation_source, // Option<String> -> TEXT or NULL
s.year_isolated, // Option<i64> -> INTEGER or NULL
])?;That is the entire reason the column types in the schema are TEXT and INTEGER rather than TEXT NOT NULL and INTEGER NOT NULL. The test insert_handles_null_year checks exactly this round-trip.
Solution
The full reference lives in day7/ex-strain-write-solution/src/main.rs. Key pieces:
pub fn create_schema(conn: &Connection) -> Result<()> {
conn.execute_batch(
"CREATE TABLE strain (
strain_id INTEGER PRIMARY KEY,
strain_name TEXT NOT NULL,
species TEXT NOT NULL,
isolation_source TEXT,
year_isolated INTEGER
);
CREATE TABLE assay (
assay_id INTEGER PRIMARY KEY,
strain_id INTEGER NOT NULL REFERENCES strain(strain_id),
medium TEXT NOT NULL,
od600_24h REAL,
date_measured TEXT
);",
)
}
pub fn insert_strains(conn: &mut Connection, strains: &[StrainInput]) -> Result<usize> {
let tx = conn.transaction()?;
let mut n = 0;
{
let mut stmt = tx.prepare(
"INSERT INTO strain (strain_name, species, isolation_source, year_isolated)
VALUES (?1, ?2, ?3, ?4)",
)?;
for s in strains {
stmt.execute(params![
s.strain_name,
s.species,
s.isolation_source,
s.year_isolated,
])?;
n += 1;
}
}
tx.commit()?;
Ok(n)
}Where this leads
- Exercise 05 — read both tables back, join them, produce a per-strain summary. Read + write together.
- In a real pipeline the inserter would also handle upserts (
INSERT ... ON CONFLICT(...) DO UPDATE) and batching (one transaction per 10 000 rows, not per file). Out of scope today; same shape of code. - Stream-friendly inserts: instead of materialising the whole
Vec<StrainInput>first, you can callstmt.execute(...)directly from the parser’sforloop. Drops peak memory from O(file) to O(1). Worth doing once you have a TSV bigger than RAM.