Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ooopsnake/sqlx-sqlite-demo
https://github.com/ooopsnake/sqlx-sqlite-demo
Last synced: 3 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/ooopsnake/sqlx-sqlite-demo
- Owner: ooopSnake
- License: gpl-2.0
- Created: 2022-02-17T02:49:04.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-02-17T03:05:27.000Z (almost 3 years ago)
- Last Synced: 2024-11-21T01:12:10.485Z (2 months ago)
- Language: Rust
- Size: 16.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Rust sqlx sqlite demo
```rust
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let pool = sqlite::SqlitePoolOptions::new()
.max_connections(5)
.connect("sqlite:local.db?mode=rwc").await?;
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS t_example (
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER
)
"#).execute(&pool).await.expect("create table");let mut tx = pool.begin().await.expect("begin tx");
for idx in 1..=2 {
sqlx::query(r#"INSERT INTO t_example(id,name,age) VALUES(?,?,?)
ON CONFLICT(id) DO UPDATE
SET name=excluded.name,
age=excluded.age"#)
.bind(idx)
.bind(format!("foo_{}", idx))
.bind(idx * 100)
.execute(&mut tx).await.expect(&format!("insert idx:{} in tx", idx));
}
tx.commit().await.expect("tx commit");
let example_stream =
sqlx::query_as::<_, Example>(r#"SELECT id,name,age FROM t_example WHERE 1=1"#)
.fetch(&pool);
example_stream.try_for_each(|it| {
dbg!(it);
futures::future::ready(Ok(()))
}).await.expect("for each query result");
Ok(())
}
```