Crate rusqlite

source ·
Expand description

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

extern crate rusqlite;
extern crate time;

use rusqlite::types::ToSql;
use rusqlite::{Connection, NO_PARAMS};
use time::Timespec;

#[derive(Debug)]
struct Person {
    id: i32,
    name: String,
    time_created: Timespec,
    data: Option<Vec<u8>>,
}

fn main() {
    let conn = Connection::open_in_memory().unwrap();

    conn.execute(
        "CREATE TABLE person (
                  id              INTEGER PRIMARY KEY,
                  name            TEXT NOT NULL,
                  time_created    TEXT NOT NULL,
                  data            BLOB
                  )",
        NO_PARAMS,
    )
    .unwrap();
    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)",
        &[&me.name as &ToSql, &me.time_created, &me.data],
    )
    .unwrap();

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

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

Modules

Online SQLite backup API.
Incremental BLOB I/O.
Create or redefine SQL functions.
Run-Time Limits
Tracing and profiling functions. Error and warning log.
Traits dealing with SQLite data types.
Create virtual tables.

Structs

An iterator over the mapped resulting rows of a query, with an Error type unifying with Error.
Cacheable statement.
A connection to a SQLite database.
Allows interrupting a long-running computation.
RAII guard temporarily enabling SQLite extensions to be loaded.
An iterator over the mapped resulting rows of a query.
Flags for opening SQLite database connections. See sqlite3_open_v2 for details.
A single result row of a query.
An handle for the resulting rows of a query.
Represents a savepoint on a database connection.
A prepared statement.
Represents a transaction on a database connection.

Enums

Name for a database within a SQLite connection.
Options for how a Transaction or Savepoint should behave when it is dropped.
Enum listing possible errors from rusqlite.
Error Codes
Options for transaction behavior. See BEGIN TRANSACTION for details.

Constants

To be used when your statement has no parameter.

Traits

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

Functions

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.
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. If you are sure the runtime version is compatible with the build-time version for your usage, you can bypass the version check by calling this function before your first connection attempt.
Returns the SQLite version as a string; e.g., "3.16.2" for version 3.16.2.
Returns the SQLite version as an integer; e.g., 3016002 for version 3.16.2.

Type Definitions

A typedef of the result returned by many methods.