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 17:23:22 -04:00
|
|
|
Open a connection, create a table, and insert a couple of rows:
|
2015-08-03 16:12:09 -04:00
|
|
|
|
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-08-03 17:23:22 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
Select a row from the table:
|
2015-06-14 11:37:56 -04:00
|
|
|
|
2015-08-03 17:23:22 -04:00
|
|
|
```rust
|
2015-08-03 17:12:15 -04:00
|
|
|
connection.iterate("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
|
|
|
```
|
|
|
|
|
2015-08-03 17:23:22 -04:00
|
|
|
The same query using a prepared statement:
|
2015-07-30 08:12:43 -04:00
|
|
|
|
|
|
|
```rust
|
2015-08-03 16:52:51 -04:00
|
|
|
use sqlite::State;
|
|
|
|
|
2015-08-03 16:12:09 -04:00
|
|
|
let mut statement = connection.prepare("
|
2015-08-03 17:23:22 -04:00
|
|
|
SELECT * FROM users WHERE age > ?
|
2015-08-03 16:12:09 -04:00
|
|
|
").unwrap();
|
|
|
|
|
2015-08-03 17:23:22 -04:00
|
|
|
statement.bind(1, 50).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());
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-08-03 17:23:22 -04:00
|
|
|
The same query example using a cursor, which is a wrapper over a prepared
|
|
|
|
statement:
|
2015-08-03 16:52:51 -04:00
|
|
|
|
|
|
|
```rust
|
|
|
|
use sqlite::Value;
|
|
|
|
|
2015-08-03 17:10:30 -04:00
|
|
|
let mut cursor = connection.prepare("
|
2015-08-03 17:23:22 -04:00
|
|
|
SELECT * FROM users WHERE age > ?
|
2015-08-03 21:21:44 -04:00
|
|
|
").unwrap().cursor();
|
2015-08-03 16:52:51 -04:00
|
|
|
|
2015-08-03 17:23:22 -04:00
|
|
|
cursor.bind(&[Value::Integer(50)]).unwrap();
|
2015-08-03 16:52:51 -04:00
|
|
|
|
2015-08-03 17:10:30 -04:00
|
|
|
while let Some(row) = cursor.next().unwrap() {
|
2015-08-03 16:52:51 -04:00
|
|
|
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
|