mirror of
https://github.com/fluencelabs/sqlite-wasm-connector
synced 2025-04-25 08:42:14 +00:00
Add an illustrative example
This commit is contained in:
parent
d17b371c4d
commit
83fb866c60
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
target
|
*.sqlite3
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
target
|
||||||
|
39
README.md
39
README.md
@ -4,6 +4,45 @@ The package provides an interface to [SQLite][1].
|
|||||||
|
|
||||||
## [Documentation][doc]
|
## [Documentation][doc]
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
The example given below can be ran using the following command:
|
||||||
|
|
||||||
|
```
|
||||||
|
cargo run --example workflow
|
||||||
|
```
|
||||||
|
|
||||||
|
```rust
|
||||||
|
extern crate sqlite;
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let path = setup();
|
||||||
|
let mut database = sqlite::open(&path).unwrap();
|
||||||
|
|
||||||
|
database.execute(r#"
|
||||||
|
CREATE TABLE `users` (id INTEGER, name VARCHAR(255));
|
||||||
|
INSERT INTO `users` (id, name) VALUES (1, 'Alice');
|
||||||
|
"#, None).unwrap();
|
||||||
|
|
||||||
|
database.execute("SELECT * FROM `users`;", Some(&mut |pairs| -> bool {
|
||||||
|
for (ref column, ref value) in pairs {
|
||||||
|
println!("{} = {}", column, value);
|
||||||
|
}
|
||||||
|
true
|
||||||
|
})).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup() -> PathBuf {
|
||||||
|
let path = PathBuf::from("database.sqlite3");
|
||||||
|
if std::fs::metadata(&path).is_ok() {
|
||||||
|
std::fs::remove_file(&path).unwrap();
|
||||||
|
}
|
||||||
|
path
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
1. Fork the project.
|
1. Fork the project.
|
||||||
|
28
examples/workflow.rs
Normal file
28
examples/workflow.rs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
extern crate sqlite;
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let path = setup();
|
||||||
|
let mut database = sqlite::open(&path).unwrap();
|
||||||
|
|
||||||
|
database.execute(r#"
|
||||||
|
CREATE TABLE `users` (id INTEGER, name VARCHAR(255));
|
||||||
|
INSERT INTO `users` (id, name) VALUES (1, 'Alice');
|
||||||
|
"#, None).unwrap();
|
||||||
|
|
||||||
|
database.execute("SELECT * FROM `users`;", Some(&mut |pairs| -> bool {
|
||||||
|
for (ref column, ref value) in pairs {
|
||||||
|
println!("{} = {}", column, value);
|
||||||
|
}
|
||||||
|
true
|
||||||
|
})).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup() -> PathBuf {
|
||||||
|
let path = PathBuf::from("database.sqlite3");
|
||||||
|
if std::fs::metadata(&path).is_ok() {
|
||||||
|
std::fs::remove_file(&path).unwrap();
|
||||||
|
}
|
||||||
|
path
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user