Skip to main content
A Connection is obtained by calling db.connect(). Each connection has its own transaction state. Multiple connections to the same Database are supported and can run concurrently.
let conn = db.connect()?;

db.connect()

Opens a new connection to the database. Returns Result<Connection>

conn.query(sql, params)

Prepares and executes a SQL statement and returns a Rows iterator. Designed for SELECT and any statement that returns rows.
let mut rows = conn.query("SELECT id, name FROM users WHERE id = ?", [1_i64]).await?;
while let Some(row) = rows.next().await? {
    let id: i64 = row.get(0)?;
    let name: String = row.get(1)?;
}
sql
impl AsRef<str>
required
The SQL statement to execute.
params
impl IntoParams
required
Bind parameters. Pass () for no parameters, a slice/array for positional parameters, or turso::params::Params::Named for named parameters.
Returns Result<Rows>

conn.execute(sql, params)

Prepares and executes a SQL statement, discarding any rows. Returns the number of rows affected.
let affected = conn
    .execute("DELETE FROM users WHERE id = ?", [42_i64])
    .await?;
println!("{affected} row(s) deleted");
sql
impl AsRef<str>
required
The SQL statement to execute.
params
impl IntoParams
required
Bind parameters.
Returns Result<u64> — number of rows affected.

conn.execute_batch(sql)

Executes a semicolon-separated batch of SQL statements. Useful for DDL scripts.
conn.execute_batch("
    CREATE TABLE a (x INTEGER);
    CREATE TABLE b (y TEXT);
").await?;
sql
impl AsRef<str>
required
One or more SQL statements separated by semicolons.
Returns Result<()>

conn.prepare(sql)

Compiles a SQL statement into a reusable Statement.
let mut stmt = conn.prepare("INSERT INTO users (email) VALUES (?)").await?;
stmt.execute(["[email protected]"]).await?;
stmt.execute(["[email protected]"]).await?;
sql
impl AsRef<str>
required
The SQL statement to compile.
Returns Result<Statement> — see Statement.

conn.prepare_cached(sql)

Same as prepare, but caches the compiled statement on the connection for re-use. Returns Result<Statement>

Transactions

Transactions are created from a mutable connection reference. The transaction Derefs to Connection, so you can call query and execute on it directly.
let mut conn = db.connect()?;
let tx = conn.transaction().await?;

tx.execute("INSERT INTO users (email) VALUES (?)", ["[email protected]"]).await?;
tx.execute("INSERT INTO users (email) VALUES (?)", ["[email protected]"]).await?;

tx.commit().await?;

conn.transaction()

Begins a new DEFERRED transaction. Returns Result<Transaction<'_>>

conn.transaction_with_behavior(behavior)

Begins a transaction with an explicit behavior.
behavior
TransactionBehavior
required
One of TransactionBehavior::Deferred, TransactionBehavior::Immediate, or TransactionBehavior::Exclusive.
Returns Result<Transaction<'_>>

conn.unchecked_transaction()

Begins a transaction without requiring &mut self. Attempting to open a nested transaction will return an error at runtime. Returns Result<Transaction<'_>>

conn.set_transaction_behavior(behavior)

Changes the default transaction behavior used by conn.transaction() and conn.unchecked_transaction().

Transaction methods

MethodDescription
tx.commit().awaitCommit the transaction
tx.rollback().awaitRoll back the transaction
tx.finish().awaitCommit or roll back according to the DropBehavior setting
tx.set_drop_behavior(behavior)Control what happens when Transaction is dropped
tx.drop_behavior()Return the current DropBehavior
Transactions roll back by default when dropped without calling commit() or finish().

Pragma helpers

conn.pragma_query(name, callback)

Runs PRAGMA <name> and calls callback for each returned row.
conn.pragma_query("journal_mode", |row| {
    println!("journal mode: {:?}", row.get_value(0));
    Ok(())
}).await?;

conn.pragma_update(name, value)

Runs PRAGMA <name> = <value> and returns any rows produced. Returns Result<Vec<Row>>

Other methods

MethodReturnsDescription
conn.last_insert_rowid()i64Rowid of the last inserted row
conn.is_autocommit()Result<bool>Whether the connection is in autocommit mode
conn.busy_timeout(duration)Result<()>Set how long to retry a busy database before returning an error
conn.cacheflush()Result<()>Flush dirty pages to the WAL

Build docs developers (and LLMs) love