mirror of
https://github.com/fluencelabs/sqlite-wasm-connector
synced 2025-06-28 14:31:33 +00:00
Simplify the example
This commit is contained in:
71
src/lib.rs
71
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::<String>(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]) {
|
||||
|
Reference in New Issue
Block a user