Crate sqlite3 [] [src]

rust-sqlite3 is a rustic binding to the sqlite3 API.

Three layers of API are provided:

  • mod ffi provides exhaustive, though unsafe, bindgen bindings for libsqlite.h.
  • mod core provides a minimal safe interface to the basic sqlite3 API.
  • mod types provides ToSql/FromSql traits, and the library provides convenient query() and update() APIs.

The following example demonstrates opening a database, executing DDL, and using the high-level query() and update() API. Note the use of Result and try!() for error handling.

extern crate time;
extern crate sqlite3;
 
use time::Timespec;
 
use sqlite3::{
    DatabaseConnection,
    Query,
    ResultRow,
    ResultRowAccess,
    SqliteResult,
    StatementUpdate,
};
 
#[derive(Debug)]
struct Person {
    id: i32,
    name: String,
    time_created: Timespec,
    // TODO: data: Option<Vec<u8>>
}
 
pub fn main() {
    match io() {
        Ok(ppl) => println!("Found people: {:?}", ppl),
        Err(oops) => panic!(oops)
    }
}
 
fn io() -> SqliteResult<Vec<Person>> {
    let mut conn = try!(DatabaseConnection::in_memory());
 
    try!(conn.exec("CREATE TABLE person (
                 id              SERIAL PRIMARY KEY,
                 name            VARCHAR NOT NULL,
                 time_created    TIMESTAMP NOT NULL
               )"));
 
    let me = Person {
        id: 0,
        name: format!("Dan"),
        time_created: time::get_time(),
    };
    {
        let mut tx = try!(conn.prepare("INSERT INTO person (name, time_created)
                           VALUES ($1, $2)"));
        let changes = try!(tx.update(&[&me.name, &me.time_created]));
        assert_eq!(changes, 1);
    }
 
    let mut stmt = try!(conn.prepare("SELECT id, name, time_created FROM person"));
 
    let to_person = |row: &mut ResultRow| Ok(
        Person {
            id: row.get("id"),
            name: row.get("name"),
            time_created: row.get(2)
        });
    let ppl = try!(stmt.query(&[], to_person));
    ppl.collect()
}

Reexports

pub use core::Access;
pub use core::DatabaseConnection;
pub use core::PreparedStatement;
pub use core::ResultSet;
pub use core::ResultRow;
pub use core::ColIx;
pub use core::ParamIx;
pub use types::FromSql;
pub use types::ToSql;

Modules

access

Access to open sqlite3 database by filename.

core

A minimal safe interface to sqlite3's basic API.

ffi

bindgen-bindings to libsqlite3

types

Type conversions for binding parameters and getting query results.

Structs

QueryResults

An iterator over transformed query results

SqliteError

Error results

Enums

ColumnType
SqliteErrorCode

Traits

Query

Mix in query() convenience function.

QueryEach

Mix in query_each() convenience function.

QueryFold

Mix in query_fold() convenience function.

ResultRowAccess

Access result columns of a row by name or numeric index.

RowIndex

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

StatementUpdate

Mix in update() convenience function.

Type Definitions

SqliteResult

The type used for returning and propagating sqlite3 errors.