87 lines
2.3 KiB
Markdown
Raw Normal View History

# SQLite [![Package][package-img]][package-url] [![Documentation][documentation-img]][documentation-url]
2015-05-28 17:21:43 -04:00
The package provides an interface to [SQLite][1].
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
connection
.execute(
"
CREATE TABLE users (name TEXT, age INTEGER);
2019-05-21 19:28:29 +02:00
INSERT INTO users VALUES ('Alice', 42);
INSERT INTO users VALUES ('Bob', 69);
",
)
.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
connection
.iterate("SELECT * FROM users WHERE age > 50", |pairs| {
for &(column, value) in pairs.iter() {
println!("{} = {}", column, value.unwrap());
}
true
})
.unwrap();
2015-07-30 08:12:43 -04: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;
let mut statement = connection
2017-08-23 06:06:04 +02:00
.prepare("SELECT * FROM users WHERE age > ?")
.unwrap();
2015-08-03 16:12:09 -04:00
2015-08-03 17:23:22 -04:00
statement.bind(1, 50).unwrap();
2015-07-04 09:27:13 -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 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;
let mut cursor = connection
2017-08-23 06:06:04 +02:00
.prepare("SELECT * FROM users WHERE age > ?")
.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
2017-08-22 17:51:45 +02:00
[build-img]: https://travis-ci.org/stainless-steel/sqlite.svg?branch=master
[build-url]: https://travis-ci.org/stainless-steel/sqlite
[documentation-img]: https://docs.rs/sqlite/badge.svg
[documentation-url]: https://docs.rs/sqlite
[package-img]: https://img.shields.io/crates/v/sqlite.svg
[package-url]: https://crates.io/crates/sqlite