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].
|
|
|
|
|
2016-11-05 11:34:15 +01:00
|
|
|
## [Documentation][documentation]
|
2015-05-28 17:24:45 -04:00
|
|
|
|
2015-05-29 09:25:39 -04:00
|
|
|
## Example
|
|
|
|
|
2016-06-16 10:55:14 +02:00
|
|
|
Open a connection, create a table, and insert some 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
|
|
|
```
|
|
|
|
|
2016-06-16 10:55:14 +02:00
|
|
|
Select some rows and process them one by one as plain text:
|
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
|
|
|
```
|
|
|
|
|
2016-06-16 10:55:14 +02:00
|
|
|
The same query using a prepared statement, which is much more efficient than the
|
|
|
|
previous technique:
|
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());
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-06-16 10:55:14 +02:00
|
|
|
The same query using a cursor, which is a wrapper around a prepared statement
|
|
|
|
providing the concept of row and featuring all-at-once binding:
|
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 22:02:46 -04:00
|
|
|
println!("name = {}", row[0].as_string().unwrap());
|
|
|
|
println!("age = {}", row[1].as_integer().unwrap());
|
2015-07-04 09:27:13 -04:00
|
|
|
}
|
2015-05-29 09:25:39 -04:00
|
|
|
```
|
|
|
|
|
2016-02-27 13:05:42 +01:00
|
|
|
## Contribution
|
2015-05-28 17:21:43 -04:00
|
|
|
|
2016-02-27 13:05:42 +01:00
|
|
|
Your contribution is highly appreciated. Do not hesitate to open an issue or a
|
|
|
|
pull request. Note that any contribution submitted for inclusion in the project
|
|
|
|
will be licensed according to the terms given in [LICENSE.md](LICENSE.md).
|
2015-05-28 17:21:43 -04:00
|
|
|
|
|
|
|
[1]: https://www.sqlite.org
|
2015-05-28 17:24:45 -04:00
|
|
|
|
2016-11-05 11:34:15 +01:00
|
|
|
[documentation]: https://docs.rs/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
|
2016-06-17 13:08:24 +02:00
|
|
|
[version-img]: https://img.shields.io/crates/v/sqlite.svg
|
|
|
|
[version-url]: https://crates.io/crates/sqlite
|