Last active
February 17, 2023 07:57
-
-
Save RahulDas-dev/b0551f74289277e7fa00e9c3c1369956 to your computer and use it in GitHub Desktop.
Sqlx::Sqlite Integration with Rust
This file contains 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
/* | |
[dependencies] | |
sqlx = { version = "0.5.9", features = [ "runtime-async-std-native-tls", "sqlite" ] } | |
async-std = { version = "1.6", features = [ "attributes" ] } | |
futures = "0.3.18" | |
*/ | |
use std::result::Result; | |
use sqlx::{sqlite::SqliteQueryResult, Sqlite, SqlitePool, migrate::MigrateDatabase}; | |
async fn cretea_schema(db_url:&str) -> Result<SqliteQueryResult, sqlx::Error> { | |
let pool = SqlitePool::connect(&db_url).await?; | |
let qry = | |
"PRAGMA foreign_keys = ON ; | |
CREATE TABLE IF NOT EXISTS settings | |
( | |
settings_id INTEGER PRIMARY KEY NOT NULL, | |
description TEXT NOT NULL, | |
created_on DATETIME DEFAULT (datetime('now','localtime')), | |
updated_on DATETIME DEFAULT (datetime('now','localtime')), | |
done BOOLEAN NOT NULL DEFAULT 0 | |
); | |
CREATE TABLE IF NOT EXISTS project | |
( | |
project_id INTEGER PRIMARY KEY AUTOINCREMENT, | |
product_name TEXT , | |
created_on DATETIME DEFAULT (datetime('now','localtime')), | |
updated_on DATETIME DEFAULT (datetime('now','localtime')), | |
img_directory TEXT NOT NULL, | |
out_directory TEXT NOT NULL, | |
status TEXT NOT NULL, | |
settings_id INTEGER NOT NULL DEFAULT 1, | |
FOREIGN KEY (settings_id) REFERENCES settings (settings_id) ON UPDATE SET NULL ON DELETE SET NULL | |
);"; | |
let result = sqlx::query(&qry).execute(&pool).await; | |
pool.close().await; | |
return result; | |
} | |
#[async_std::main] | |
async fn main(){ | |
let db_url = String::from("sqlite://sqlite.db"); | |
if !Sqlite::database_exists(&db_url).await.unwrap_or(false) { | |
Sqlite::create_database(&db_url).await.unwrap(); | |
match cretea_schema(&db_url).await { | |
Ok(_) => println!("Database created Sucessfully"), | |
Err(e) => panic!("{}",e), | |
} | |
} | |
let instances = SqlitePool::connect(&db_url).await.unwrap(); | |
let qry ="INSERT INTO settings (description) VALUES($1)"; | |
let result = sqlx::query(&qry).bind("testing").execute(&instances).await; | |
instances.close().await; | |
println!("{:?}", result); | |
} |
Thanks a lot! I run the example todos and get information "can not open the file".But your demo works well.Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!