Crate sqlite3

Source
Expand description

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()
}

Re-exports§

Modules§

  • Access to open sqlite3 database by filename.
  • A minimal safe interface to sqlite3’s basic API.
  • bindgen-bindings to libsqlite3
  • Type conversions for binding parameters and getting query results.

Structs§

Enums§

Traits§

  • Mix in query() convenience function.
  • Mix in query_each() convenience function.
  • Mix in query_fold() convenience function.
  • Access result columns of a row by name or numeric index.
  • A trait implemented by types that can index into columns of a row.
  • Mix in update() convenience function.

Type Aliases§

  • The type used for returning and propagating sqlite3 errors.