mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-04-14 00:10:13 +00:00
update todo examples to 0.4 and use migrations
* update tests/x.py * add examples/x.py
This commit is contained in:
@@ -1,27 +1,41 @@
|
||||
# TODOs Example
|
||||
|
||||
## Setup
|
||||
|
||||
1. Declare the database URL
|
||||
|
||||
```
|
||||
export DATABASE_URL="sqlite:todos.db"
|
||||
```
|
||||
|
||||
2. Create the database.
|
||||
|
||||
```
|
||||
$ sqlx db create
|
||||
```
|
||||
|
||||
3. Run sql migrations
|
||||
|
||||
```
|
||||
$ sqlx migrate run
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Declare the database URL:
|
||||
Add a todo
|
||||
|
||||
```
|
||||
export DATABASE_URL="sqlite:///path/to/this/directory/todos.db"
|
||||
cargo run -- add "todo description"
|
||||
```
|
||||
|
||||
Create the database:
|
||||
Complete a todo.
|
||||
|
||||
```
|
||||
sqlite3 todos.db
|
||||
cargo run -- done <todo id>
|
||||
```
|
||||
|
||||
Load the database schema (using the SQLite CLI interface opened from the previous command):
|
||||
List all todos
|
||||
|
||||
```
|
||||
sqlite> .read schema.sql
|
||||
cargo run
|
||||
```
|
||||
|
||||
Use `.exit` to leave the SQLite CLI. Then, to run this example:
|
||||
|
||||
- Add a todo: `cargo run -- add "todo description"`
|
||||
- Complete a todo: `cargo run -- done <todo id>`
|
||||
- List all todos: `cargo run`
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
CREATE TABLE IF NOT EXISTS todos
|
||||
(
|
||||
id INTEGER PRIMARY KEY NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
done BOOLEAN NOT NULL DEFAULT 0
|
||||
);
|
||||
@@ -1,5 +0,0 @@
|
||||
CREATE TABLE IF NOT EXISTS todos (
|
||||
id INTEGER PRIMARY KEY NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
done BOOLEAN NOT NULL DEFAULT 0
|
||||
);
|
||||
@@ -1,5 +1,5 @@
|
||||
use anyhow::Context;
|
||||
use sqlx::SqlitePool;
|
||||
use sqlx::sqlite::SqlitePool;
|
||||
use sqlx::Done;
|
||||
use std::env;
|
||||
use structopt::StructOpt;
|
||||
|
||||
@@ -18,10 +18,7 @@ enum Command {
|
||||
#[async_std::main]
|
||||
#[paw::main]
|
||||
async fn main(args: Args) -> anyhow::Result<()> {
|
||||
let pool = SqlitePool::new(
|
||||
&env::var("DATABASE_URL").context("`DATABASE_URL` must be set to run this example")?,
|
||||
)
|
||||
.await?;
|
||||
let pool = SqlitePool::connect(&env::var("DATABASE_URL")?).await?;
|
||||
|
||||
match args.cmd {
|
||||
Some(Command::Add { description }) => {
|
||||
@@ -49,22 +46,19 @@ async fn main(args: Args) -> anyhow::Result<()> {
|
||||
async fn add_todo(pool: &SqlitePool, description: String) -> anyhow::Result<i64> {
|
||||
let mut conn = pool.acquire().await?;
|
||||
|
||||
// Insert the TODO, then obtain the ID of this row
|
||||
sqlx::query!(
|
||||
// Insert the task, then obtain the ID of this row
|
||||
let id = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO todos ( description )
|
||||
VALUES ( $1 )
|
||||
VALUES ( ?1 )
|
||||
"#,
|
||||
description
|
||||
)
|
||||
.execute(&mut conn)
|
||||
.await?;
|
||||
.await?
|
||||
.last_insert_rowid();
|
||||
|
||||
let rec: (i64,) = sqlx::query_as("SELECT last_insert_rowid()")
|
||||
.fetch_one(&mut conn)
|
||||
.await?;
|
||||
|
||||
Ok(rec.0)
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
async fn complete_todo(pool: &SqlitePool, id: i64) -> anyhow::Result<bool> {
|
||||
@@ -72,12 +66,13 @@ async fn complete_todo(pool: &SqlitePool, id: i64) -> anyhow::Result<bool> {
|
||||
r#"
|
||||
UPDATE todos
|
||||
SET done = TRUE
|
||||
WHERE id = $1
|
||||
WHERE id = ?1
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
.await?
|
||||
.rows_affected();
|
||||
|
||||
Ok(rows_affected > 0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user