Browse Source

Insert and delete

master
Julio Biason 4 years ago
parent
commit
c64a45f49e
  1. 4
      sqlxtest/db/migrations/202105261221_create_table.sql
  2. 23
      sqlxtest/src/main.rs

4
sqlxtest/db/migrations/202105261221_create_table.sql

@ -1,5 +1,5 @@
-- Create table -- Create table
CREATE TABLE testing ( CREATE TABLE testing (
id TEXT PRIMARY KEY, id INTEGER NOT NULL PRIMARY KEY,
label TEXT label TEXT NOT NULL
) )

23
sqlxtest/src/main.rs

@ -1,9 +1,10 @@
use sqlx::sqlite::SqliteConnectOptions; use sqlx::sqlite::SqliteConnectOptions;
use sqlx::sqlite::SqlitePoolOptions; use sqlx::sqlite::SqlitePoolOptions;
use std::env;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), sqlx::Error> { async fn main() -> Result<(), sqlx::Error> {
println!("Open"); println!("Open database");
let pool = SqlitePoolOptions::new() let pool = SqlitePoolOptions::new()
.connect_with( .connect_with(
SqliteConnectOptions::new() SqliteConnectOptions::new()
@ -11,8 +12,24 @@ async fn main() -> Result<(), sqlx::Error> {
.create_if_missing(true), .create_if_missing(true),
) )
.await?; .await?;
println!("Migrate");
sqlx::migrate!("db/migrations").run(&pool).await?; 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(()) Ok(())
} }

Loading…
Cancel
Save