2014-11-05 00:32:06 +08:00
|
|
|
# Rusqlite
|
|
|
|
|
2014-11-05 01:48:33 +08:00
|
|
|
[![Build Status](https://api.travis-ci.org/jgallagher/rusqlite.svg?branch=master)](https://travis-ci.org/jgallagher/rusqlite)
|
|
|
|
|
2014-11-11 01:56:32 +08:00
|
|
|
Rusqlite is an ergonomic wrapper for using SQLite from Rust. It attempts to expose
|
2014-11-05 04:36:04 +08:00
|
|
|
an interface similar to [rust-postgres](https://github.com/sfackler/rust-postgres). View the full
|
2015-05-04 09:50:09 +08:00
|
|
|
[API documentation](http://jgallagher.github.io/rusqlite/rusqlite/index.html).
|
2014-11-05 00:32:06 +08:00
|
|
|
|
|
|
|
```rust
|
|
|
|
extern crate rusqlite;
|
|
|
|
extern crate time;
|
|
|
|
|
|
|
|
use time::Timespec;
|
2015-12-13 02:50:12 +08:00
|
|
|
use rusqlite::Connection;
|
2014-11-05 00:32:06 +08:00
|
|
|
|
2015-02-04 07:59:58 +08:00
|
|
|
#[derive(Debug)]
|
2014-11-05 00:32:06 +08:00
|
|
|
struct Person {
|
|
|
|
id: i32,
|
|
|
|
name: String,
|
|
|
|
time_created: Timespec,
|
|
|
|
data: Option<Vec<u8>>
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2015-12-13 02:50:12 +08:00
|
|
|
let conn = Connection::open_in_memory().unwrap();
|
2014-11-05 00:32:06 +08:00
|
|
|
|
|
|
|
conn.execute("CREATE TABLE person (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
name TEXT NOT NULL,
|
|
|
|
time_created TEXT NOT NULL,
|
|
|
|
data BLOB
|
2014-12-04 00:14:49 +08:00
|
|
|
)", &[]).unwrap();
|
2014-11-05 00:32:06 +08:00
|
|
|
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, &me.time_created, &me.data]).unwrap();
|
|
|
|
|
|
|
|
let mut stmt = conn.prepare("SELECT id, name, time_created, data FROM person").unwrap();
|
2015-05-07 21:36:29 +08:00
|
|
|
let mut person_iter = stmt.query_map(&[], |row| {
|
|
|
|
Person {
|
2014-11-05 00:32:06 +08:00
|
|
|
id: row.get(0),
|
|
|
|
name: row.get(1),
|
|
|
|
time_created: row.get(2),
|
|
|
|
data: row.get(3)
|
2015-05-07 21:36:29 +08:00
|
|
|
}
|
|
|
|
}).unwrap();
|
|
|
|
|
|
|
|
for person in person_iter {
|
|
|
|
println!("Found person {:?}", person.unwrap());
|
2014-11-05 00:32:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-12-13 05:04:11 +08:00
|
|
|
### Optional Features
|
|
|
|
|
|
|
|
Rusqlite provides several features that are behind [Cargo
|
|
|
|
features](http://doc.crates.io/manifest.html#the-features-section). They are:
|
|
|
|
|
|
|
|
* [`load_extension`](http://jgallagher.github.io/rusqlite/rusqlite/struct.LoadExtensionGuard.html)
|
|
|
|
allows loading dynamic library-based SQLite extensions.
|
|
|
|
* [`backup`](http://jgallagher.github.io/rusqlite/rusqlite/backup/index.html)
|
|
|
|
allows use of SQLite's online backup API.
|
|
|
|
* [`functions`](http://jgallagher.github.io/rusqlite/rusqlite/functions/index.html)
|
|
|
|
allows you to load Rust closures into SQLite connections for use in queries.
|
|
|
|
* [`trace`](http://jgallagher.github.io/rusqlite/rusqlite/trace/index.html)
|
|
|
|
allows hooks into SQLite's tracing and profiling APIs.
|
2015-12-15 05:24:11 +08:00
|
|
|
* [`blob`](http://jgallagher.github.io/rusqlite/rusqlite/blob/index.html)
|
|
|
|
gives `std::io::{Read, Write, Seek}` access to SQL BLOBs.
|
2015-12-13 05:04:11 +08:00
|
|
|
|
2015-12-13 03:11:24 +08:00
|
|
|
### Design of Rows and Row
|
2014-11-05 00:32:06 +08:00
|
|
|
|
|
|
|
To retrieve the result rows from a query, SQLite requires you to call
|
|
|
|
[sqlite3_step()](https://www.sqlite.org/c3ref/step.html) on a prepared statement. You can only
|
|
|
|
retrieve the values of the "current" row. From the Rust point of view, this means that each row
|
2014-11-11 01:56:32 +08:00
|
|
|
is only valid until the next row is fetched. [rust-sqlite3](https://github.com/dckc/rust-sqlite3)
|
|
|
|
solves this the correct way with lifetimes. However, this means that the result rows do not
|
|
|
|
satisfy the [Iterator](http://doc.rust-lang.org/std/iter/trait.Iterator.html) trait, which means
|
|
|
|
you cannot (as easily) loop over the rows, or use many of the helpful Iterator methods like `map`
|
|
|
|
and `filter`.
|
|
|
|
|
2015-12-13 03:11:24 +08:00
|
|
|
Instead, Rusqlite's `Rows` handle does conform to `Iterator`. It ensures safety by
|
2014-11-11 01:56:32 +08:00
|
|
|
performing checks at runtime to ensure you do not try to retrieve the values of a "stale" row, and
|
|
|
|
will panic if you do so. A specific example that will panic:
|
2014-11-05 00:32:06 +08:00
|
|
|
|
|
|
|
```rust
|
2015-12-13 03:06:03 +08:00
|
|
|
fn bad_function_will_panic(conn: &Connection) -> Result<i64> {
|
2014-11-05 00:32:06 +08:00
|
|
|
let mut stmt = try!(conn.prepare("SELECT id FROM my_table"));
|
2014-12-04 00:14:49 +08:00
|
|
|
let mut rows = try!(stmt.query(&[]));
|
2014-11-05 00:32:06 +08:00
|
|
|
|
|
|
|
let row0 = try!(rows.next().unwrap());
|
2014-11-11 23:36:53 +08:00
|
|
|
// row 0 is valid now...
|
2014-11-05 00:32:06 +08:00
|
|
|
|
|
|
|
let row1 = try!(rows.next().unwrap());
|
|
|
|
// row 0 is now STALE, and row 1 is valid
|
|
|
|
|
|
|
|
let my_id = row0.get(0); // WILL PANIC because row 0 is stale
|
|
|
|
Ok(my_id)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
There are other, less obvious things that may result in a panic as well, such as calling
|
2015-12-13 03:11:24 +08:00
|
|
|
`collect()` on a `Rows` and then trying to use the collected rows.
|
2014-11-05 00:32:06 +08:00
|
|
|
|
2015-05-12 04:44:32 +08:00
|
|
|
Strongly consider using the method `query_map()` instead, if you can.
|
2015-12-01 23:55:01 +08:00
|
|
|
`query_map()` returns an iterator over rows-mapped-to-some-type. This
|
2015-05-12 04:44:32 +08:00
|
|
|
iterator does not have any of the above issues with panics due to attempting to
|
|
|
|
access stale rows.
|
2015-05-07 21:36:29 +08:00
|
|
|
|
2014-11-05 00:32:06 +08:00
|
|
|
## Author
|
|
|
|
|
|
|
|
John Gallagher, johnkgallagher@gmail.com
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
Rusqlite is available under the MIT license. See the LICENSE file for more info.
|