Code Monkey home page Code Monkey logo

sqlx-sqlite-demo's Introduction

Rust sqlx sqlite demo

#[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(())
}

sqlx-sqlite-demo's People

Contributors

ooopsnake avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.