[][src]Crate rusqlite

Rusqlite is an ergonomic wrapper for using SQLite from Rust. It attempts to expose an interface similar to rust-postgres.

use rusqlite::{params, Connection, Result};
use time::Timespec;

#[derive(Debug)]
struct Person {
    id: i32,
    name: String,
    time_created: Timespec,
    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,
                  time_created    TEXT NOT NULL,
                  data            BLOB
                  )",
        params![],
    )?;
    let me = Person {
        id: 0,
        name: "Steven".to_string(),
        time_created: time::get_time(),
        data: None,
    };
    conn.execute(
        "INSERT INTO person (name, time_created, data)
                  VALUES (?1, ?2, ?3)",
        params![me.name, me.time_created, me.data],
    )?;

    let mut stmt = conn.prepare("SELECT id, name, time_created, data FROM person")?;
    let person_iter = stmt.query_map(params![], |row| {
        Ok(Person {
            id: row.get(0)?,
            name: row.get(1)?,
            time_created: row.get(2)?,
            data: row.get(3)?,
        })
    })?;

    for person in person_iter {
        println!("Found person {:?}", person.unwrap());
    }
    Ok(())
}

Re-exports

pub use libsqlite3_sys as ffi;

Modules

backup

Online SQLite backup API.

blob

Incremental BLOB I/O.

config

Configure database connections

functions

Create or redefine SQL functions.

limits

Run-Time Limits

trace

Tracing and profiling functions. Error and warning log.

types

Traits dealing with SQLite data types.

vtab

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 heterogeneous lists of parameters as a &[&dyn ToSql].

Structs

AndThenRows

An iterator over the mapped resulting rows of a query, with an Error type unifying with Error.

CachedStatement

Cacheable statement.

Column

Information about 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.

MappedRows

An iterator over the mapped resulting rows of a query.

OpenFlags

Flags for opening SQLite database connections. See sqlite3_open_v2 for details.

Row

A single result row of a query.

Rows

An handle 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.

Constants

NO_PARAMS

To be used when your statement has no parameter.

Traits

OptionalExtension

See the method documentation.

RowIndex

A trait implemented by types that can index into columns of a row.

ToSql

A trait for types that can be converted into SQLite values.

Functions

bypass_sqlite_initialization

rusqlite's check for a safe SQLite threading mode requires SQLite 3.7.0 or later. If you are running against a SQLite older than that, rusqlite attempts to ensure safety by performing configuration and initialization of SQLite itself the first time you attempt to open a connection. By default, rusqlite panics if that initialization fails, since that could mean SQLite has been initialized in single-thread mode.

bypass_sqlite_version_check

rusqlite performs a one-time check that the runtime SQLite version is at least as new as the version of SQLite found when rusqlite was built. Bypassing this check may be dangerous; e.g., if you use features of SQLite that are not present in the runtime version.

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 Definitions

Result

A typedef of the result returned by many methods.