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.
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.
The SQL statement to execute.
Bind parameters. Pass
() for no parameters, a slice/array for positional parameters, or turso::params::Params::Named for named parameters.Result<Rows>
conn.execute(sql, params)
Prepares and executes a SQL statement, discarding any rows. Returns the number of rows affected.
The SQL statement to execute.
Bind parameters.
Result<u64> — number of rows affected.
conn.execute_batch(sql)
Executes a semicolon-separated batch of SQL statements. Useful for DDL scripts.
One or more SQL statements separated by semicolons.
Result<()>
conn.prepare(sql)
Compiles a SQL statement into a reusable Statement.
The SQL statement to compile.
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 transactionDerefs to Connection, so you can call query and execute on it directly.
conn.transaction()
Begins a new DEFERRED transaction.
Returns Result<Transaction<'_>>
conn.transaction_with_behavior(behavior)
Begins a transaction with an explicit behavior.
One of
TransactionBehavior::Deferred, TransactionBehavior::Immediate, or TransactionBehavior::Exclusive.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
| Method | Description |
|---|---|
tx.commit().await | Commit the transaction |
tx.rollback().await | Roll back the transaction |
tx.finish().await | Commit 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 |
commit() or finish().
Pragma helpers
conn.pragma_query(name, callback)
Runs PRAGMA <name> and calls callback for each returned row.
conn.pragma_update(name, value)
Runs PRAGMA <name> = <value> and returns any rows produced.
Returns Result<Vec<Row>>
Other methods
| Method | Returns | Description |
|---|---|---|
conn.last_insert_rowid() | i64 | Rowid 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 |