From c64a45f49e81abf2fcc9be262711e5f5b131dc3f Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Fri, 28 May 2021 13:46:17 -0300 Subject: [PATCH] Insert and delete --- .../migrations/202105261221_create_table.sql | 4 ++-- sqlxtest/src/main.rs | 23 ++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/sqlxtest/db/migrations/202105261221_create_table.sql b/sqlxtest/db/migrations/202105261221_create_table.sql index 29e377d..54ccb3d 100644 --- a/sqlxtest/db/migrations/202105261221_create_table.sql +++ b/sqlxtest/db/migrations/202105261221_create_table.sql @@ -1,5 +1,5 @@ -- Create table CREATE TABLE testing ( - id TEXT PRIMARY KEY, - label TEXT + id INTEGER NOT NULL PRIMARY KEY, + label TEXT NOT NULL ) diff --git a/sqlxtest/src/main.rs b/sqlxtest/src/main.rs index a2f8eb7..47a7696 100644 --- a/sqlxtest/src/main.rs +++ b/sqlxtest/src/main.rs @@ -1,9 +1,10 @@ use sqlx::sqlite::SqliteConnectOptions; use sqlx::sqlite::SqlitePoolOptions; +use std::env; #[tokio::main] async fn main() -> Result<(), sqlx::Error> { - println!("Open"); + println!("Open database"); let pool = SqlitePoolOptions::new() .connect_with( SqliteConnectOptions::new() @@ -11,8 +12,24 @@ async fn main() -> Result<(), sqlx::Error> { .create_if_missing(true), ) .await?; - println!("Migrate"); sqlx::migrate!("db/migrations").run(&pool).await?; - println!("Done"); + + let command = env::args().nth(1).unwrap(); + println!("Command: \"{}\"", command); + if command == "add" { + let value = env::args().nth(2).unwrap(); + println!("Should add \"{}\"", value); + sqlx::query(r#"INSERT INTO testing (label) VALUES (?)"#) + .bind(value) + .execute(&pool) + .await?; + } else if command == "remove" { + let value = env::args().nth(2).unwrap(); + println!("Should remove \"{}\"", value); + sqlx::query(r#"DELETE FROM testing WHERE label = ?"#) + .bind(value) + .execute(&pool) + .await?; + } Ok(()) }