2015-06-03 22:14:48 -04:00
|
|
|
# SQLite [![Version][version-img]][version-url] [![Status][status-img]][status-url]
|
2015-05-28 17:21:43 -04:00
|
|
|
|
|
|
|
The package provides an interface to [SQLite][1].
|
|
|
|
|
2015-05-28 17:24:45 -04:00
|
|
|
## [Documentation][doc]
|
|
|
|
|
2015-05-29 09:25:39 -04:00
|
|
|
## Example
|
|
|
|
|
2015-08-03 16:12:09 -04:00
|
|
|
Create a table, insert a couple of rows, and fetch one:
|
|
|
|
|
2015-05-29 09:25:39 -04:00
|
|
|
```rust
|
2015-07-04 08:53:26 -04:00
|
|
|
let connection = sqlite::open(":memory:").unwrap();
|
2015-06-14 11:37:56 -04:00
|
|
|
|
2015-07-04 09:29:46 -04:00
|
|
|
connection.execute("
|
2015-08-03 16:12:09 -04:00
|
|
|
CREATE TABLE users (name TEXT, age INTEGER);
|
|
|
|
INSERT INTO users (name, age) VALUES ('Alice', 42);
|
|
|
|
INSERT INTO users (name, age) VALUES ('Bob', 69);
|
2015-07-04 09:29:46 -04:00
|
|
|
").unwrap();
|
2015-06-14 11:37:56 -04:00
|
|
|
|
2015-08-03 16:12:09 -04:00
|
|
|
connection.process("SELECT * FROM users WHERE age > 50", |pairs| {
|
2015-06-14 11:37:56 -04:00
|
|
|
for &(column, value) in pairs.iter() {
|
|
|
|
println!("{} = {}", column, value.unwrap());
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}).unwrap();
|
2015-07-30 08:12:43 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
The same example using prepared statements:
|
|
|
|
|
|
|
|
```rust
|
2015-08-03 16:52:51 -04:00
|
|
|
use sqlite::State;
|
|
|
|
|
2015-07-30 08:12:43 -04:00
|
|
|
let connection = sqlite::open(":memory:").unwrap();
|
|
|
|
|
|
|
|
connection.execute("
|
2015-08-03 16:12:09 -04:00
|
|
|
CREATE TABLE users (name TEXT, age INTEGER)
|
|
|
|
").unwrap();
|
|
|
|
|
|
|
|
let mut statement = connection.prepare("
|
|
|
|
INSERT INTO users (name, age) VALUES (?, ?)
|
|
|
|
").unwrap();
|
|
|
|
|
|
|
|
statement.bind(1, "Alice").unwrap();
|
|
|
|
statement.bind(2, 42).unwrap();
|
2015-08-03 17:05:03 -04:00
|
|
|
assert_eq!(statement.next().unwrap(), State::Done);
|
2015-08-03 16:12:09 -04:00
|
|
|
|
|
|
|
statement.reset().unwrap();
|
|
|
|
|
|
|
|
statement.bind(1, "Bob").unwrap();
|
|
|
|
statement.bind(2, 69).unwrap();
|
2015-08-03 17:05:03 -04:00
|
|
|
assert_eq!(statement.next().unwrap(), State::Done);
|
2015-07-30 08:12:43 -04:00
|
|
|
|
|
|
|
let mut statement = connection.prepare("
|
2015-08-03 16:12:09 -04:00
|
|
|
SELECT * FROM users WHERE age > 50
|
2015-07-30 08:12:43 -04:00
|
|
|
").unwrap();
|
2015-07-04 09:27:13 -04:00
|
|
|
|
2015-08-03 17:05:03 -04:00
|
|
|
while let State::Row = statement.next().unwrap() {
|
2015-08-03 16:52:51 -04:00
|
|
|
println!("name = {}", statement.read::<String>(0).unwrap());
|
|
|
|
println!("age = {}", statement.read::<i64>(1).unwrap());
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The same example using iterators:
|
|
|
|
|
|
|
|
```rust
|
|
|
|
use sqlite::Value;
|
|
|
|
|
|
|
|
let connection = sqlite::open(":memory:").unwrap();
|
|
|
|
|
|
|
|
connection.execute("
|
|
|
|
CREATE TABLE users (name TEXT, age INTEGER)
|
|
|
|
").unwrap();
|
|
|
|
|
|
|
|
let mut iterator = connection.prepare("
|
|
|
|
INSERT INTO users (name, age) VALUES (?, ?)
|
|
|
|
").unwrap().into_iter().unwrap();
|
|
|
|
|
2015-08-03 17:05:03 -04:00
|
|
|
iterator.bind(&[
|
2015-08-03 16:52:51 -04:00
|
|
|
Value::String("Alice".to_string()), Value::Integer(42),
|
|
|
|
]).unwrap();
|
|
|
|
|
2015-08-03 17:05:03 -04:00
|
|
|
iterator.bind(&[
|
2015-08-03 16:52:51 -04:00
|
|
|
Value::String("Bob".to_string()), Value::Integer(69),
|
|
|
|
]).unwrap();
|
|
|
|
|
|
|
|
let mut iterator = connection.prepare("
|
|
|
|
SELECT * FROM users WHERE age > 50
|
|
|
|
").unwrap().into_iter().unwrap();
|
|
|
|
|
|
|
|
while let Some(row) = iterator.next().unwrap() {
|
|
|
|
match (&row[0], &row[1]) {
|
|
|
|
(&Value::String(ref name), &Value::Integer(age)) => {
|
|
|
|
println!("name = {}", name);
|
|
|
|
println!("age = {}", age);
|
|
|
|
},
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2015-07-04 09:27:13 -04:00
|
|
|
}
|
2015-05-29 09:25:39 -04:00
|
|
|
```
|
|
|
|
|
2015-05-28 17:21:43 -04:00
|
|
|
## Contributing
|
|
|
|
|
|
|
|
1. Fork the project.
|
|
|
|
2. Implement your idea.
|
|
|
|
3. Open a pull request.
|
|
|
|
|
|
|
|
[1]: https://www.sqlite.org
|
2015-05-28 17:24:45 -04:00
|
|
|
|
2015-07-30 08:15:08 -04:00
|
|
|
[version-img]: http://stainless-steel.github.io/images/crates.svg
|
2015-06-03 22:14:48 -04:00
|
|
|
[version-url]: https://crates.io/crates/sqlite
|
2015-05-28 17:24:45 -04:00
|
|
|
[status-img]: https://travis-ci.org/stainless-steel/sqlite.svg?branch=master
|
|
|
|
[status-url]: https://travis-ci.org/stainless-steel/sqlite
|
|
|
|
[doc]: https://stainless-steel.github.io/sqlite
|