mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-22 03:23:05 +00:00
Upgrade rusqlite
`NO_PARAMS` has been deprecated. For homogeneous parameters, `[value1, value2, ...]` can be used directly. For heterogeneous parameters, `params![value1, value2, ...]` is preferrred.
This commit is contained in:
parent
752b035c1a
commit
4978d0120e
4 changed files with 19 additions and 21 deletions
|
@ -43,7 +43,7 @@ rayon = "1.0"
|
|||
regex = "1.0"
|
||||
reqwest = { version = "0.10", features = ["blocking", "json", "stream"] }
|
||||
ring = "0.16.11"
|
||||
rusqlite = { version = "0.22", features = ["chrono"] }
|
||||
rusqlite = { version = "0.25", features = ["chrono"] }
|
||||
same-file = "1.0"
|
||||
select = "0.4"
|
||||
semver = "0.9"
|
||||
|
|
|
@ -9,7 +9,6 @@ Use the `rusqlite` crate to open SQLite databases. See
|
|||
|
||||
```rust,edition2018,no_run
|
||||
use rusqlite::{Connection, Result};
|
||||
use rusqlite::NO_PARAMS;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let conn = Connection::open("cats.db")?;
|
||||
|
@ -19,7 +18,7 @@ fn main() -> Result<()> {
|
|||
id integer primary key,
|
||||
name text not null unique
|
||||
)",
|
||||
NO_PARAMS,
|
||||
[],
|
||||
)?;
|
||||
conn.execute(
|
||||
"create table if not exists cats (
|
||||
|
@ -27,7 +26,7 @@ fn main() -> Result<()> {
|
|||
name text not null,
|
||||
color_id integer not null references cat_colors(id)
|
||||
)",
|
||||
NO_PARAMS,
|
||||
[],
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
|
@ -36,4 +35,4 @@ fn main() -> Result<()> {
|
|||
|
||||
[`Connection::open`]: https://docs.rs/rusqlite/*/rusqlite/struct.Connection.html#method.open
|
||||
|
||||
[documentation]: https://github.com/jgallagher/rusqlite#user-content-notes-on-building-rusqlite-and-libsqlite3-sys
|
||||
[documentation]: https://github.com/rusqlite/rusqlite#user-content-notes-on-building-rusqlite-and-libsqlite3-sys
|
||||
|
|
|
@ -7,8 +7,7 @@ This recipe inserts data into `cat_colors` and `cats` tables using the [`execute
|
|||
|
||||
```rust,no_run
|
||||
|
||||
use rusqlite::NO_PARAMS;
|
||||
use rusqlite::{Connection, Result};
|
||||
use rusqlite::{params, Connection, Result};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -26,25 +25,25 @@ fn main() -> Result<()> {
|
|||
|
||||
for (color, catnames) in &cat_colors {
|
||||
conn.execute(
|
||||
"INSERT INTO cat_colors (name) values (?1)",
|
||||
&[&color.to_string()],
|
||||
"INSERT INTO cat_colors (name) VALUES (?1)",
|
||||
[color],
|
||||
)?;
|
||||
let last_id: String = conn.last_insert_rowid().to_string();
|
||||
let last_id = conn.last_insert_rowid();
|
||||
|
||||
for cat in catnames {
|
||||
conn.execute(
|
||||
"INSERT INTO cats (name, color_id) values (?1, ?2)",
|
||||
&[&cat.to_string(), &last_id],
|
||||
params![cat, last_id],
|
||||
)?;
|
||||
}
|
||||
}
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT c.name, cc.name from cats c
|
||||
"SELECT c.name, cc.name FROM cats c
|
||||
INNER JOIN cat_colors cc
|
||||
ON cc.id = c.color_id;",
|
||||
)?;
|
||||
|
||||
let cats = stmt.query_map(NO_PARAMS, |row| {
|
||||
let cats = stmt.query_map([], |row| {
|
||||
Ok(Cat {
|
||||
name: row.get(0)?,
|
||||
color: row.get(1)?,
|
||||
|
|
|
@ -13,7 +13,7 @@ a duplicate color is made, the transaction rolls back.
|
|||
|
||||
|
||||
```rust,edition2018,no_run
|
||||
use rusqlite::{Connection, Result, NO_PARAMS};
|
||||
use rusqlite::{Connection, Result};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let mut conn = Connection::open("cats.db")?;
|
||||
|
@ -29,9 +29,9 @@ fn main() -> Result<()> {
|
|||
fn successful_tx(conn: &mut Connection) -> Result<()> {
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
tx.execute("delete from cat_colors", NO_PARAMS)?;
|
||||
tx.execute("insert into cat_colors (name) values (?1)", &[&"lavender"])?;
|
||||
tx.execute("insert into cat_colors (name) values (?1)", &[&"blue"])?;
|
||||
tx.execute("delete from cat_colors", [])?;
|
||||
tx.execute("insert into cat_colors (name) values (?1)", ["lavender"])?;
|
||||
tx.execute("insert into cat_colors (name) values (?1)", ["blue"])?;
|
||||
|
||||
tx.commit()
|
||||
}
|
||||
|
@ -39,10 +39,10 @@ fn successful_tx(conn: &mut Connection) -> Result<()> {
|
|||
fn rolled_back_tx(conn: &mut Connection) -> Result<()> {
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
tx.execute("delete from cat_colors", NO_PARAMS)?;
|
||||
tx.execute("insert into cat_colors (name) values (?1)", &[&"lavender"])?;
|
||||
tx.execute("insert into cat_colors (name) values (?1)", &[&"blue"])?;
|
||||
tx.execute("insert into cat_colors (name) values (?1)", &[&"lavender"])?;
|
||||
tx.execute("delete from cat_colors", [])?;
|
||||
tx.execute("insert into cat_colors (name) values (?1)", ["lavender"])?;
|
||||
tx.execute("insert into cat_colors (name) values (?1)", ["blue"])?;
|
||||
tx.execute("insert into cat_colors (name) values (?1)", ["lavender"])?;
|
||||
|
||||
tx.commit()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue