sqlite / sqlx + async

I am working on geolocation bot. This bot should send to users nearest showplaces in Tashkent. I created some demo DB using Python and Django. And I have an old telegram bot that sends nearest cafes in Tbilisi. But it doesn’t use any DB just a hardcode…


This content originally appeared on DEV Community and was authored by Antonov Mike

I am working on geolocation bot. This bot should send to users nearest showplaces in Tashkent. I created some demo DB using Python and Django. And I have an old telegram bot that sends nearest cafes in Tbilisi. But it doesn’t use any DB just a hardcoded catalog. So I spend an evening to try Rust crates sqlite and sqlx. I’ve already tested both of them with my bot: both works. Here are few lines of code to show how it works with different libraries.

sqlx

[dependencies]
anyhow = "1.0"
tokio = { version = "1", features = ["full"] }
sqlx = { version = "0.6", features = ["sqlite", "runtime-tokio-native-tls"] }
use sqlx::{sqlite::SqlitePool, Row};

struct Museums {
    name: String,
    summ: String,
    sche: String,
    pric: String,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let content = database().await;

    for i in content {
        println!("{}\"\nWork hours: {}\nTicket price = {}\n\n{}\n", i.name, i.sche, i.pric, i.summ)
    }

    Ok(())
}
#[allow(unused)]
async fn database() -> Vec<Museums> {
    let mut museums: Vec<Museums> = vec![];

    let pool = match SqlitePool::connect("db.sqlite3").await {
        Ok(it) => it,
        Err(err) => return museums,
    };

    let base = sqlx::query("SELECT * FROM catalog_museum").fetch_all(&pool).await;
    for i in base.unwrap() {
        let temp_struct = Museums {
            name: i.get(1),
            summ: i.get(2),
            sche: i.get(3),
            pric: i.get(4),
        };
        museums.push(temp_struct)
     }

    museums
}

sqlite

[dependencies]
anyhow = "1.0"
tokio = { version = "1", features = ["full"] }
sqlite = "0.30.4"
use sqlite::State;

struct Museums {
    name: String,
    summ: String,
    sche: String,
    pric: String,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let content = database().await;
    for i in content {
        println!("{}\"\nWork hours: {}\nTicket price = {}\n\n{}\n", i.name, i.sche, i.pric, i.summ)
    }

    Ok(())
}

async fn database() -> Vec<Museums> {
    let connection = sqlite::open("db.sqlite3").unwrap();
    let query = "SELECT * FROM catalog_museum";
    let mut statement = connection.prepare(query).unwrap();

    let mut museums: Vec<Museums> = vec![];

    while let Ok(State::Row) = statement.next() {
        let temp_sctruct = Museums {
            name: statement.read::<String, _>("name").unwrap(),
            summ: statement.read::<String, _>("summary").unwrap(),
            sche: statement.read::<String, _>("schedule").unwrap(),
            pric: statement.read::<String, _>("price").unwrap(),
        };
        museums.push(temp_sctruct)
    }

    museums
}

I’ll probably choose the last one because it’s more comfortable to use real names instead of indexes.


This content originally appeared on DEV Community and was authored by Antonov Mike


Print Share Comment Cite Upload Translate Updates
APA

Antonov Mike | Sciencx (2023-03-10T17:27:17+00:00) sqlite / sqlx + async. Retrieved from https://www.scien.cx/2023/03/10/sqlite-sqlx-async/

MLA
" » sqlite / sqlx + async." Antonov Mike | Sciencx - Friday March 10, 2023, https://www.scien.cx/2023/03/10/sqlite-sqlx-async/
HARVARD
Antonov Mike | Sciencx Friday March 10, 2023 » sqlite / sqlx + async., viewed ,<https://www.scien.cx/2023/03/10/sqlite-sqlx-async/>
VANCOUVER
Antonov Mike | Sciencx - » sqlite / sqlx + async. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/10/sqlite-sqlx-async/
CHICAGO
" » sqlite / sqlx + async." Antonov Mike | Sciencx - Accessed . https://www.scien.cx/2023/03/10/sqlite-sqlx-async/
IEEE
" » sqlite / sqlx + async." Antonov Mike | Sciencx [Online]. Available: https://www.scien.cx/2023/03/10/sqlite-sqlx-async/. [Accessed: ]
rf:citation
» sqlite / sqlx + async | Antonov Mike | Sciencx | https://www.scien.cx/2023/03/10/sqlite-sqlx-async/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.