rust-cookbook
rust-cookbook copied to clipboard
rusqlite first example does not compile
trafficstars
jlpoole@ryzdesk ~/adsb/tests/rust $ cargo new rusqlite1 --bin
Created binary (application) `rusqlite1` package
jlpoole@ryzdesk ~/adsb/tests/rust $ cd rusqlite1/
jlpoole@ryzdesk ~/adsb/tests/rust/rusqlite1 $ cargo add rusqlite
Updating crates.io index
Adding rusqlite v0.30.0 to dependencies.
Features:
- array
- backup
- blob
- buildtime_bindgen
- bundled
- bundled-full
- bundled-sqlcipher
- bundled-sqlcipher-vendored-openssl
- bundled-windows
- chrono
- collation
- column_decltype
- csv
- csvtab
- extra_check
- functions
- hooks
- i128_blob
- in_gecko
- limits
- load_extension
- loadable_extension
- modern-full
- modern_sqlite
- release_memory
- rusqlite-macros
- serde_json
- serialize
- series
- session
- sqlcipher
- time
- trace
- unlock_notify
- url
- uuid
- vtab
- wasm32-wasi-vfs
- window
- winsqlite3
- with-asan
Updating crates.io index
jlpoole@ryzdesk ~/adsb/tests/rust/rusqlite1 $ cat -n src/main.rs
1 //
2 // ref: https://rust-lang-nursery.github.io/rust-cookbook/database/sqlite.html
3 //
4 // cargo add rusqlite
5 //
6 use rusqlite::{Connection, Result};
7 use rusqlite::NO_PARAMS;
8
9 fn main() -> Result<()> {
10 let conn = Connection::open("cats.db")?;
11
12 conn.execute(
13 "create table if not exists cat_colors (
14 id integer primary key,
15 name text not null unique
16 )",
17 NO_PARAMS,
18 )?;
19 conn.execute(
20 "create table if not exists cats (
21 id integer primary key,
22 name text not null,
23 color_id integer not null references cat_colors(id)
24 )",
25 NO_PARAMS,
26 )?;
27
28 Ok(())
29 }
jlpoole@ryzdesk ~/adsb/tests/rust/rusqlite1 $ date; cargo run
Tue Jan 2 05:35:08 PM PST 2024
Compiling rusqlite1 v0.1.0 (/home/jlpoole/adsb/tests/rust/rusqlite1)
error[E0432]: unresolved import `rusqlite::NO_PARAMS`
--> src/main.rs:7:5
|
7 | use rusqlite::NO_PARAMS;
| ^^^^^^^^^^^^^^^^^^^ no `NO_PARAMS` in the root
For more information about this error, try `rustc --explain E0432`.
error: could not compile `rusqlite1` (bin "rusqlite1") due to previous error
jlpoole@ryzdesk ~/adsb/tests/rust/rusqlite1 $
Here's the diff of the modified example so that it works. There are 2 modifications and 1 deletion.
jlpoole@ryzdesk ~/adsb/tests/rust/rusqlite1 $ diff src/original_main.rs src/main.rs
7c7
< use rusqlite::NO_PARAMS;
---
> //use rusqlite::NO_PARAMS;
17c17
< NO_PARAMS,
---
> (),
25c25
< NO_PARAMS,
---
> (),
jlpoole@ryzdesk ~/adsb/tests/rust/rusqlite1 $ date; cargo run
Tue Jan 2 05:44:52 PM PST 2024
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/rusqlite1`
jlpoole@ryzdesk ~/adsb/tests/rust/rusqlite1 $
Likewise, the 2nd example suffers from the illegal NO_PARAMS variable. I replaced NO_PARAMS with "()" and now the example with the 2nd code added works.
Tue Jan 2 05:55:26 PM PST 2024
Compiling rusqlite1 v0.1.0 (/home/jlpoole/adsb/tests/rust/rusqlite1)
error[E0425]: cannot find value `NO_PARAMS` in this scope
--> src/main.rs:59:31
|
59 | let cats = stmt.query_map(NO_PARAMS, |row| {
| ^^^^^^^^^ not found in this scope
For more information about this error, try `rustc --explain E0425`.
error: could not compile `rusqlite1` (bin "rusqlite1") due to previous error
jlpoole@ryzdesk ~/adsb/tests/rust/rusqlite1 $ sed -i s/NO_PARAMS/\(\)/ src/main.rs
jlpoole@ryzdesk ~/adsb/tests/rust/rusqlite1 $ cat src/main.rs |grep NO_PARAMS
jlpoole@ryzdesk ~/adsb/tests/rust/rusqlite1 $ date;cargo run
Tue Jan 2 05:56:59 PM PST 2024
Compiling rusqlite1 v0.1.0 (/home/jlpoole/adsb/tests/rust/rusqlite1)
warning: fields `name` and `color` are never read
--> src/main.rs:12:5
|
11 | struct Cat {
| --- fields in this struct
12 | name: String,
| ^^^^
13 | color: String,
| ^^^^^
|
= note: `Cat` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
= note: `#[warn(dead_code)]` on by default
warning: `rusqlite1` (bin "rusqlite1") generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.32s
Running `target/debug/rusqlite1`
Error: SqliteFailure(Error { code: ConstraintViolation, extended_code: 2067 }, Some("UNIQUE constraint failed: cat_colors.name"))
jlpoole@ryzdesk ~/adsb/tests/rust/rusqlite1 $ rm cats.db
jlpoole@ryzdesk ~/adsb/tests/rust/rusqlite1 $ date;cargo run
Tue Jan 2 05:57:17 PM PST 2024
warning: fields `name` and `color` are never read
--> src/main.rs:12:5
|
11 | struct Cat {
| --- fields in this struct
12 | name: String,
| ^^^^
13 | color: String,
| ^^^^^
|
= note: `Cat` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
= note: `#[warn(dead_code)]` on by default
warning: `rusqlite1` (bin "rusqlite1") generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/rusqlite1`
Found cat Ok(Cat { name: "Oreo", color: "Black" })
Found cat Ok(Cat { name: "Biscuit", color: "Black" })
Found cat Ok(Cat { name: "Tigger", color: "Blue" })
Found cat Ok(Cat { name: "Sammy", color: "Blue" })
jlpoole@ryzdesk ~/adsb/tests/rust/rusqlite1 $