Crate rusqlite

Source
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
backupbackup
Online SQLite backup API.
blobblob
Incremental BLOB I/O.
config
Configure database connections
functionsfunctions
Create or redefine SQL functions.
hookshooks
Commit, Data Change and Rollback Notification Callbacks
limitslimits
Run-Time Limits
serializeserialize
Serialize a database.
tracetrace
Tracing and profiling functions. Error and warning log.
types
Traits dealing with SQLite data types.
vtabvtab
Create virtual tables.

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].
prepare_and_bindrusqlite-macros
Captured identifiers in SQL
prepare_cached_and_bindrusqlite-macros
Captured identifiers in SQL

Structs§

AndThenRows
An iterator over the mapped resulting rows of a query, with an Error type unifying with Error.
Batch
Batch fallible iterator
CachedStatement
Cacheable statement.
Column
Information about a column of a SQLite query.
ColumnMetadata
Metadata about the origin of a column of a SQLite query
Connection
A connection to a SQLite database.
InterruptHandle
Allows interrupting a long-running computation.
LoadExtensionGuard
RAII guard temporarily enabling SQLite extensions to be loaded.
Map
F is used to transform the streaming iterator into a fallible iterator.
MappedRows
An iterator over the mapped resulting rows of a query.
OpenFlags
Flags for opening SQLite database connections. See sqlite3_open_v2 for details.
ParamsFromIter
Adapter type which allows any iterator over ToSql values to implement Params.
PrepFlags
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§

DatabaseName
Name for a database within a SQLite connection.
DropBehavior
Options for how a Transaction or Savepoint should behave when it is dropped.
Error
Enum listing possible errors from rusqlite.
ErrorCode
Error Codes
StatementStatus
Prepared statement status counters.
TransactionBehavior
Options for transaction behavior. See BEGIN TRANSACTION for details.
TransactionState
Transaction state of a database

Constants§

MAIN_DB
Shorthand for DatabaseName::Main.
TEMP_DB
Shorthand for DatabaseName::Temp.

Traits§

BindIndex
A trait implemented by types that can index into parameters of a statement.
OptionalExtension
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§

params_from_iter
Constructor function for a ParamsFromIter. See its documentation for more.
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., 3016002 for version 3.16.2.

Type Aliases§

Result
A typedef of the result returned by many methods.