❮ SQLite
❯
- memory
- open
- execute
- INSERT
- SELECT
SQLite in memory example
- In this example we use an in-memory database.
- That's useful for the examples and it can be also useful if we have a lot of data on the disk that we would like analyze using SQL statements but we don't need to store the data in a database.
- In general it is not a good idea to have values in the SQL statements, especially not variables, but in this first exampleswe use that for simplicity.
examples/sqlite/in-memory-example/Cargo.toml
[package] name = "in-memory-example" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] sqlite = "0.32.0"
examples/sqlite/in-memory-example/src/main.rs
use sqlite::State; fn main() { let connection = sqlite::open(":memory:").unwrap(); connection.execute("CREATE TABLE users (name TEXT, age INTEGER);").unwrap(); connection.execute("INSERT INTO users VALUES ('Alice', 42);").unwrap(); connection.execute("INSERT INTO users VALUES ('Bob', 69);").unwrap(); let query = "SELECT * FROM users"; //let query = "SELECT * FROM users WHERE age > 50"; let mut statement = connection.prepare(query).unwrap(); while let Ok(State::Row) = statement.next() { println!("name = {}", statement.read::<String, _>("name").unwrap()); println!("age = {}", statement.read::<i64, _>("age").unwrap()); } }
name = Alice age = 42 name = Bob age = 69