From e3222f050d6efd001ec24f4999c5c1cb781f1ea7 Mon Sep 17 00:00:00 2001 From: Ivan Ukhov Date: Thu, 30 Jul 2015 08:12:43 -0400 Subject: [PATCH] Extend the example --- README.md | 23 +++++++++++++++++++++-- src/lib.rs | 23 +++++++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e27e1ec..acac6d6 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ let connection = sqlite::open(":memory:").unwrap(); connection.execute(" CREATE TABLE `users` (id INTEGER, name VARCHAR(255)); - INSERT INTO `users` (id, name) VALUES (1, 'Alice'); + INSERT INTO `users` (id, name) VALUES (42, 'Alice'); ").unwrap(); connection.process("SELECT * FROM `users`", |pairs| { @@ -20,9 +20,28 @@ connection.process("SELECT * FROM `users`", |pairs| { } true }).unwrap(); +``` + +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)) +"); + +let mut statement = connection.prepare(" + INSERT INTO `users` (id, name) 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 sqlite::State::Row = statement.step().unwrap() { +while let 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 c7c7fee..aa901c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ //! //! connection.execute(" //! CREATE TABLE `users` (id INTEGER, name VARCHAR(255)); -//! INSERT INTO `users` (id, name) VALUES (1, 'Alice'); +//! INSERT INTO `users` (id, name) VALUES (42, 'Alice'); //! ").unwrap(); //! //! connection.process("SELECT * FROM `users`", |pairs| { @@ -16,9 +16,28 @@ //! } //! true //! }).unwrap(); +//! ``` +//! +//! 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)) +//! "); +//! +//! let mut statement = connection.prepare(" +//! INSERT INTO `users` (id, name) 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 sqlite::State::Row = statement.step().unwrap() { +//! while let State::Row = statement.step().unwrap() { //! println!("id = {}", statement.read::(0).unwrap()); //! println!("name = {}", statement.read::(1).unwrap()); //! }