Expand description
Rusqlite is an ergonomic wrapper for using SQLite from Rust.
Historically, the API was based on the one from
rust-postgres. However, the
two have diverged in many ways, and no compatibility between the two is
intended.
use rusqlite::{params, Connection, Result};
#[derive(Debug)]
struct Person {
id: i32,
name: String,
data: Option<Vec<u8>>,
}
fn main() -> Result<()> {
let conn = Connection::open_in_memory()?;
conn.execute(
"CREATE TABLE person (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
data BLOB
)",
(), // empty list of parameters.
)?;
let me = Person {
id: 0,
name: "Steven".to_string(),
data: None,
};
conn.execute(
"INSERT INTO person (name, data) VALUES (?1, ?2)",
(&me.name, &me.data),
)?;
let mut stmt = conn.prepare("SELECT id, name, data FROM person")?;
let person_iter = stmt.query_map([], |row| {
Ok(Person {
id: row.get(0)?,
name: row.get(1)?,
data: row.get(2)?,
})
})?;
for person in person_iter {
println!("Found person {:?}", person?);
}
Ok(())
}Re-exportsΒ§
pub use crate::types::ToSql;pub use fallible_iterator;pub use fallible_streaming_iterator;pub use libsqlite3_sys as ffi;
ModulesΒ§
- auto_
extension - Automatic extension loading
- bind π
- busy π
- Busy handler (when the database is locked)
- cache π
- Prepared statements cache for faster execution.
- column π
- config
- Configure database connections
- error π
- inner_
connection π - params π
- pragma π
- Pragma helpers
- raw_
statement π - row π
- statement π
- transaction π
- types
- Traits dealing with SQLite data types.
- util π
- version π
MacrosΒ§
- named_
params - A macro making it more convenient to pass lists of named parameters
as a
&[(&str, &dyn ToSql)]. - params
- A macro making it more convenient to pass longer lists of
parameters as a
&[&dyn ToSql].
StructsΒ§
- AndThen
Rows - An iterator over the mapped resulting rows of a query, with an Error type unifying with Error.
- Batch
- Batch fallible iterator
- Cached
Statement - Cacheable statement.
- Connection
- A connection to a SQLite database.
- Interrupt
Handle - Allows interrupting a long-running computation.
- Map
Fis used to transform the streaming iterator into a fallible iterator.- Mapped
Rows - An iterator over the mapped resulting rows of a query.
- Open
Flags - Flags for opening SQLite database connections. See sqlite3_open_v2 for details.
- Params
From Iter - Adapter type which allows any iterator over
ToSqlvalues to implementParams. - Prep
Flags - Prepare flags. See sqlite3_prepare_v3 for details.
- Row
- A single result row of a query.
- Rows
- A handle (lazy fallible streaming iterator) for the resulting rows of a query.
- Savepoint
- Represents a savepoint on a database connection.
- Statement
- A prepared statement.
- Transaction
- Represents a transaction on a database connection.
EnumsΒ§
- Drop
Behavior - Options for how a Transaction or Savepoint should behave when it is dropped.
- Error
- Enum listing possible errors from rusqlite.
- Error
Code - Error Codes
- Statement
Status - Prepared statement status counters.
- Transaction
Behavior - Options for transaction behavior. See BEGIN TRANSACTION for details.
- Transaction
State - Transaction state of a database
ConstantsΒ§
- MAIN_DB
- Shorthand for
Maindatabase. - STATEMENT_
CACHE_ πDEFAULT_ CAPACITY - TEMP_DB
- Shorthand for
Tempdatabase.
TraitsΒ§
- Bind
Index - A trait implemented by types that can index into parameters of a statement.
- Name
- Database, table, column, collation, function, module, vfs name
- Optional
Extension - See the method documentation.
- Params
- Trait used for sets of parameter passed into SQL statements/queries.
- RowIndex
- A trait implemented by types that can index into columns of a row.
FunctionsΒ§
- errmsg_
to_ π βstring - len_
as_ πc_ int - params_
from_ iter - Constructor function for a
ParamsFromIter. See its documentation for more. - path_
to_ πcstring - str_
for_ πsqlite - Returns
Ok((string ptr, len as c_int, SQLITE_STATIC | SQLITE_TRANSIENT))normally. Returns error if the string is too large for sqlite. Thesqlite3_destructor_typeitem is alwaysSQLITE_TRANSIENTunless the string was empty (in which case itβsSQLITE_STATIC, and the ptr is static). - to_
sqlite_ βerror - Transform Rust error to SQLite error (message and code).
- version
- Returns the SQLite version as a string; e.g.,
"3.16.2"for version 3.16.2. - version_
number - Returns the SQLite version as an integer; e.g.,
3016002for version 3.16.2.
Type AliasesΒ§
- Result
- A typedef of the result returned by many methods.