Created
May 5, 2024 10:21
-
-
Save joaofig/83c31752932dfa996bd6127642469642 to your computer and use it in GitHub Desktop.
sqlx sample code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::result::Result; | |
use sqlx::{SqlitePool, FromRow}; | |
#[derive(FromRow, Debug, PartialEq, Clone)] | |
struct LevelRange { | |
level_num: i64, | |
level_min: f64, | |
level_max: f64, | |
} | |
#[tokio::main] | |
async fn main() { | |
let db_url = String::from("sqlite:///Users/joafigu/data/ved/tile.db"); | |
let db = SqlitePool::connect(&db_url).await.unwrap(); | |
let sql = "select * from level_range;"; | |
let results: Result<Vec<LevelRange>, sqlx::Error> = sqlx::query_as(sql).fetch_all(&db).await; | |
match results { | |
Ok(rows) => { | |
for row in rows { | |
println!("{:?}", row); | |
} | |
} | |
Err(_) => { | |
println!("No rows found!") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment