From ef9793a3033d0c749a5a9c4c1ff5377643a43b08 Mon Sep 17 00:00:00 2001 From: Ivan Ukhov Date: Mon, 3 Aug 2015 17:23:22 -0400 Subject: [PATCH] Simplify the example --- README.md | 53 +++++++++------------------------------- src/lib.rs | 71 +++++++++++++++++++++++------------------------------- 2 files changed, 42 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index c3addc7..1cb7a16 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ The package provides an interface to [SQLite][1]. ## Example -Create a table, insert a couple of rows, and fetch one: +Open a connection, create a table, and insert a couple of rows: ```rust let connection = sqlite::open(":memory:").unwrap(); @@ -16,7 +16,11 @@ connection.execute(" INSERT INTO users (name, age) VALUES ('Alice', 42); INSERT INTO users (name, age) VALUES ('Bob', 69); ").unwrap(); +``` +Select a row from the table: + +```rust connection.iterate("SELECT * FROM users WHERE age > 50", |pairs| { for &(column, value) in pairs.iter() { println!("{} = {}", column, value.unwrap()); @@ -25,34 +29,16 @@ connection.iterate("SELECT * FROM users WHERE age > 50", |pairs| { }).unwrap(); ``` -The same example using prepared statements: +The same query using a prepared statement: ```rust use sqlite::State; -let connection = sqlite::open(":memory:").unwrap(); - -connection.execute(" - CREATE TABLE users (name TEXT, age INTEGER) -").unwrap(); - let mut statement = connection.prepare(" - INSERT INTO users (name, age) VALUES (?, ?) + SELECT * FROM users WHERE age > ? ").unwrap(); -statement.bind(1, "Alice").unwrap(); -statement.bind(2, 42).unwrap(); -assert_eq!(statement.next().unwrap(), State::Done); - -statement.reset().unwrap(); - -statement.bind(1, "Bob").unwrap(); -statement.bind(2, 69).unwrap(); -assert_eq!(statement.next().unwrap(), State::Done); - -let mut statement = connection.prepare(" - SELECT * FROM users WHERE age > 50 -").unwrap(); +statement.bind(1, 50).unwrap(); while let State::Row = statement.next().unwrap() { println!("name = {}", statement.read::(0).unwrap()); @@ -60,32 +46,17 @@ while let State::Row = statement.next().unwrap() { } ``` -The same example using cursors: +The same query example using a cursor, which is a wrapper over a prepared +statement: ```rust use sqlite::Value; -let connection = sqlite::open(":memory:").unwrap(); - -connection.execute(" - CREATE TABLE users (name TEXT, age INTEGER) -").unwrap(); - let mut cursor = connection.prepare(" - INSERT INTO users (name, age) VALUES (?, ?) + SELECT * FROM users WHERE age > ? ").unwrap().cursor().unwrap(); -cursor.bind(&[ - Value::String("Alice".to_string()), Value::Integer(42), -]).unwrap(); - -cursor.bind(&[ - Value::String("Bob".to_string()), Value::Integer(69), -]).unwrap(); - -let mut cursor = connection.prepare(" - SELECT * FROM users WHERE age > 50 -").unwrap().cursor().unwrap(); +cursor.bind(&[Value::Integer(50)]).unwrap(); while let Some(row) = cursor.next().unwrap() { match (&row[0], &row[1]) { diff --git a/src/lib.rs b/src/lib.rs index ab7718d..3068ff5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ //! //! ## Example //! -//! Create a table, insert a couple of rows, and fetch one: +//! Open a connection, create a table, and insert a couple of rows: //! //! ``` //! let connection = sqlite::open(":memory:").unwrap(); @@ -12,7 +12,17 @@ //! INSERT INTO users (name, age) VALUES ('Alice', 42); //! INSERT INTO users (name, age) VALUES ('Bob', 69); //! ").unwrap(); +//! ``` //! +//! Select a row from the table: +//! +//! ``` +//! # let connection = sqlite::open(":memory:").unwrap(); +//! # connection.execute(" +//! # 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.iterate("SELECT * FROM users WHERE age > 50", |pairs| { //! for &(column, value) in pairs.iter() { //! println!("{} = {}", column, value.unwrap()); @@ -21,34 +31,22 @@ //! }).unwrap(); //! ``` //! -//! The same example using prepared statements: +//! The same query using a prepared statement: //! //! ``` //! use sqlite::State; -//! -//! let connection = sqlite::open(":memory:").unwrap(); -//! -//! connection.execute(" -//! CREATE TABLE users (name TEXT, age INTEGER) -//! ").unwrap(); +//! # let connection = sqlite::open(":memory:").unwrap(); +//! # connection.execute(" +//! # CREATE TABLE users (name TEXT, age INTEGER); +//! # INSERT INTO users (name, age) VALUES ('Alice', 42); +//! # INSERT INTO users (name, age) VALUES ('Bob', 69); +//! # ").unwrap(); //! //! let mut statement = connection.prepare(" -//! INSERT INTO users (name, age) VALUES (?, ?) +//! SELECT * FROM users WHERE age > ? //! ").unwrap(); //! -//! statement.bind(1, "Alice").unwrap(); -//! statement.bind(2, 42).unwrap(); -//! assert_eq!(statement.next().unwrap(), State::Done); -//! -//! statement.reset().unwrap(); -//! -//! statement.bind(1, "Bob").unwrap(); -//! statement.bind(2, 69).unwrap(); -//! assert_eq!(statement.next().unwrap(), State::Done); -//! -//! let mut statement = connection.prepare(" -//! SELECT * FROM users WHERE age > 50 -//! ").unwrap(); +//! statement.bind(1, 50).unwrap(); //! //! while let State::Row = statement.next().unwrap() { //! println!("name = {}", statement.read::(0).unwrap()); @@ -56,32 +54,23 @@ //! } //! ``` //! -//! The same example using cursors: +//! The same query example using a cursor, which is a wrapper over a prepared +//! statement: //! //! ``` //! use sqlite::Value; -//! -//! let connection = sqlite::open(":memory:").unwrap(); -//! -//! connection.execute(" -//! CREATE TABLE users (name TEXT, age INTEGER) -//! ").unwrap(); +//! # let connection = sqlite::open(":memory:").unwrap(); +//! # connection.execute(" +//! # CREATE TABLE users (name TEXT, age INTEGER); +//! # INSERT INTO users (name, age) VALUES ('Alice', 42); +//! # INSERT INTO users (name, age) VALUES ('Bob', 69); +//! # ").unwrap(); //! //! let mut cursor = connection.prepare(" -//! INSERT INTO users (name, age) VALUES (?, ?) +//! SELECT * FROM users WHERE age > ? //! ").unwrap().cursor().unwrap(); //! -//! cursor.bind(&[ -//! Value::String("Alice".to_string()), Value::Integer(42), -//! ]).unwrap(); -//! -//! cursor.bind(&[ -//! Value::String("Bob".to_string()), Value::Integer(69), -//! ]).unwrap(); -//! -//! let mut cursor = connection.prepare(" -//! SELECT * FROM users WHERE age > 50 -//! ").unwrap().cursor().unwrap(); +//! cursor.bind(&[Value::Integer(50)]).unwrap(); //! //! while let Some(row) = cursor.next().unwrap() { //! match (&row[0], &row[1]) {