From a6de11a00036f51f6fe6ad3ff3593befd62a8f24 Mon Sep 17 00:00:00 2001 From: Ivan Ukhov Date: Mon, 3 Aug 2015 16:12:09 -0400 Subject: [PATCH] Detail the example --- README.md | 37 ++++++++++++++++++++++++------------- src/lib.rs | 37 ++++++++++++++++++++++++------------- tests/lib.rs | 2 +- 3 files changed, 49 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index acde497..381418f 100644 --- a/README.md +++ b/README.md @@ -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::(0).unwrap()); println!("name = {}", statement.read::(1).unwrap()); } diff --git a/src/lib.rs b/src/lib.rs index 9a792ea..05a52b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::(0).unwrap()); //! println!("name = {}", statement.read::(1).unwrap()); //! } diff --git a/tests/lib.rs b/tests/lib.rs index a6fb9b4..914c6ef 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -145,7 +145,7 @@ fn statement_read() { fn setup>(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