Skip to content

Instantly share code, notes, and snippets.

@joaofig
Created May 5, 2024 10:21
Show Gist options
  • Save joaofig/83c31752932dfa996bd6127642469642 to your computer and use it in GitHub Desktop.
Save joaofig/83c31752932dfa996bd6127642469642 to your computer and use it in GitHub Desktop.
sqlx sample code
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