Fix bugs where the sample code does not compile and run correctly.

This commit is contained in:
Arteiimis 2024-01-29 16:52:09 +08:00
parent 752b035c1a
commit 454fbf11ea
3 changed files with 6 additions and 10 deletions

View file

@ -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(())

View file

@ -6,8 +6,6 @@
This recipe inserts data into `cat_colors` and `cats` tables using the [`execute`] method of `Connection`. First, the data is inserted into the `cat_colors` table. After a record for a color is inserted, [`last_insert_rowid`] method of `Connection` is used to get `id` of the last color inserted. This `id` is used while inserting data into the `cats` table. Then, the select query is prepared using the [`prepare`] method which gives a [`statement`] struct. Then, query is executed using [`query_map`] method of [`statement`].
```rust,no_run
use rusqlite::NO_PARAMS;
use rusqlite::{Connection, Result};
use std::collections::HashMap;
@ -44,7 +42,7 @@ fn main() -> Result<()> {
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)?,

View file

@ -11,9 +11,8 @@ In the following example, colors add to a table having
a unique constraint on the color name. When an attempt to insert
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,7 +28,7 @@ fn main() -> Result<()> {
fn successful_tx(conn: &mut Connection) -> Result<()> {
let tx = conn.transaction()?;
tx.execute("delete from cat_colors", NO_PARAMS)?;
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"])?;
@ -39,7 +38,7 @@ 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("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"])?;