mirror of
https://github.com/fluencelabs/sqlite-wasm-connector
synced 2025-04-24 16:32:12 +00:00
Simplify the example
This commit is contained in:
parent
2091c9843c
commit
ef9793a303
53
README.md
53
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::<String>(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]) {
|
||||
|
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]) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user