Detail the example

This commit is contained in:
Ivan Ukhov 2015-08-03 16:12:09 -04:00
parent 75bd2d94b6
commit a6de11a000
3 changed files with 49 additions and 27 deletions

View File

@ -6,15 +6,18 @@ The package provides an interface to [SQLite][1].
## Example
Create a table, insert a couple of rows, and fetch one:
```rust
let connection = sqlite::open(":memory:").unwrap();
connection.execute("
CREATE TABLE users (id INTEGER, name VARCHAR(255));
INSERT INTO users (id, name) VALUES (42, 'Alice');
CREATE TABLE users (name TEXT, age INTEGER);
INSERT INTO users (name, age) VALUES ('Alice', 42);
INSERT INTO users (name, age) VALUES ('Bob', 69);
").unwrap();
connection.process("SELECT * FROM users", |pairs| {
connection.process("SELECT * FROM users WHERE age > 50", |pairs| {
for &(column, value) in pairs.iter() {
println!("{} = {}", column, value.unwrap());
}
@ -25,23 +28,31 @@ connection.process("SELECT * FROM users", |pairs| {
The same example using prepared statements:
```rust
use sqlite::State;
let connection = sqlite::open(":memory:").unwrap();
connection.execute("
CREATE TABLE users (id INTEGER, name VARCHAR(255))
");
CREATE TABLE users (name TEXT, age INTEGER)
").unwrap();
let mut statement = connection.prepare("
INSERT INTO users (id, name) VALUES (?, ?)
INSERT INTO users (name, age) VALUES (?, ?)
").unwrap();
statement.bind(1, 42).unwrap();
statement.bind(2, "Alice").unwrap();
assert_eq!(statement.step().unwrap(), State::Done);
let mut statement = connection.prepare("SELECT * FROM users").unwrap();
while let State::Row = statement.step().unwrap() {
statement.bind(1, "Alice").unwrap();
statement.bind(2, 42).unwrap();
assert_eq!(statement.step().unwrap(), sqlite::State::Done);
statement.reset().unwrap();
statement.bind(1, "Bob").unwrap();
statement.bind(2, 69).unwrap();
assert_eq!(statement.step().unwrap(), sqlite::State::Done);
let mut statement = connection.prepare("
SELECT * FROM users WHERE age > 50
").unwrap();
while let sqlite::State::Row = statement.step().unwrap() {
println!("id = {}", statement.read::<i64>(0).unwrap());
println!("name = {}", statement.read::<String>(1).unwrap());
}

View File

@ -2,15 +2,18 @@
//!
//! ## Example
//!
//! Create a table, insert a couple of rows, and fetch one:
//!
//! ```
//! let connection = sqlite::open(":memory:").unwrap();
//!
//! connection.execute("
//! CREATE TABLE users (id INTEGER, name VARCHAR(255));
//! INSERT INTO users (id, name) VALUES (42, 'Alice');
//! CREATE TABLE users (name TEXT, age INTEGER);
//! INSERT INTO users (name, age) VALUES ('Alice', 42);
//! INSERT INTO users (name, age) VALUES ('Bob', 69);
//! ").unwrap();
//!
//! connection.process("SELECT * FROM users", |pairs| {
//! connection.process("SELECT * FROM users WHERE age > 50", |pairs| {
//! for &(column, value) in pairs.iter() {
//! println!("{} = {}", column, value.unwrap());
//! }
@ -21,23 +24,31 @@
//! The same example using prepared statements:
//!
//! ```
//! use sqlite::State;
//!
//! let connection = sqlite::open(":memory:").unwrap();
//!
//! connection.execute("
//! CREATE TABLE users (id INTEGER, name VARCHAR(255))
//! ");
//! CREATE TABLE users (name TEXT, age INTEGER)
//! ").unwrap();
//!
//! let mut statement = connection.prepare("
//! INSERT INTO users (id, name) VALUES (?, ?)
//! INSERT INTO users (name, age) VALUES (?, ?)
//! ").unwrap();
//! statement.bind(1, 42).unwrap();
//! statement.bind(2, "Alice").unwrap();
//! assert_eq!(statement.step().unwrap(), State::Done);
//!
//! let mut statement = connection.prepare("SELECT * FROM users").unwrap();
//! while let State::Row = statement.step().unwrap() {
//! statement.bind(1, "Alice").unwrap();
//! statement.bind(2, 42).unwrap();
//! assert_eq!(statement.step().unwrap(), sqlite::State::Done);
//!
//! statement.reset().unwrap();
//!
//! statement.bind(1, "Bob").unwrap();
//! statement.bind(2, 69).unwrap();
//! assert_eq!(statement.step().unwrap(), sqlite::State::Done);
//!
//! let mut statement = connection.prepare("
//! SELECT * FROM users WHERE age > 50
//! ").unwrap();
//!
//! while let sqlite::State::Row = statement.step().unwrap() {
//! println!("id = {}", statement.read::<i64>(0).unwrap());
//! println!("name = {}", statement.read::<String>(1).unwrap());
//! }

View File

@ -145,7 +145,7 @@ fn statement_read() {
fn setup<T: AsRef<Path>>(path: T) -> Connection {
let connection = ok!(sqlite::open(path));
ok!(connection.execute("
CREATE TABLE users (id INTEGER, name VARCHAR(255), age REAL, photo BLOB);
CREATE TABLE users (id INTEGER, name TEXT, age REAL, photo BLOB);
INSERT INTO users (id, name, age, photo) VALUES (1, 'Alice', 42.69, X'4269');
"));
connection