From 454ceb34ffbc9277e3ccf63c5deb42594aae8aeb Mon Sep 17 00:00:00 2001 From: Vince Pike Date: Mon, 15 Apr 2019 16:59:34 -0400 Subject: [PATCH] Old Code Would Not Compile (#522) * Old Code Would Not Compile I researched the Chrono library and made sure the code would compile with the latest Rust 2018 setup. * Remove the DateTime part for the chrono piece * Removed Chrono to simplify example --- src/database/sqlite/initialization.md | 4 +-- src/database/sqlite/insert_select.md | 38 ++++++++++++++------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/database/sqlite/initialization.md b/src/database/sqlite/initialization.md index 5319db0..ede5731 100644 --- a/src/database/sqlite/initialization.md +++ b/src/database/sqlite/initialization.md @@ -10,7 +10,8 @@ Use the `rusqlite` crate to open SQLite databases. See ```rust,no_run extern crate rusqlite; -use rusqlite::{Connection, Result, NO_PARAMS}; +use rusqlite::{Connection, Result}; +use rusqlite::NO_PARAMS; fn main() -> Result<()> { let conn = Connection::open("cats.db")?; @@ -26,7 +27,6 @@ fn main() -> Result<()> { "create table if not exists cats ( id integer primary key, name text not null, - date_of_birth datetime, color_id integer not null references cat_colors(id) )", NO_PARAMS, diff --git a/src/database/sqlite/insert_select.md b/src/database/sqlite/insert_select.md index 17c94cb..df37d86 100644 --- a/src/database/sqlite/insert_select.md +++ b/src/database/sqlite/insert_select.md @@ -6,24 +6,22 @@ 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`]. ``` -extern crate chrono; -extern crate rusqlite; +eextern crate rusqlite; use rusqlite::{Connection, Result}; -use chrono::{DateTime, NaiveDateTime, Utc}; +use rusqlite::NO_PARAMS; use std::collections::HashMap; + #[derive(Debug)] struct Cat { name: String, - date_of_birth: DateTime, color: String } -fn main() -> Result<()> { +fn main() -> Result<()> { let conn = Connection::open("cats.db")?; - let date_of_birth = DateTime::::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc); - + let mut cat_colors = HashMap::new(); cat_colors.insert(String::from("Blue"), vec!["Tigger", "Sammy"]); cat_colors.insert(String::from("Black"), vec!["Oreo", "Biscuit"]); @@ -33,23 +31,27 @@ fn main() -> Result<()> { "INSERT INTO cat_colors (name) values (?1)", &[&color.to_string()], )?; - let last_id = conn.last_insert_rowid(); + let last_id : String = conn.last_insert_rowid().to_string(); + for cat in catnames{ conn.execute( - "INSERT INTO cats (name, date_of_birth, color_id) values (?1, ?2, ?3)", - &[&cat.to_string(), &date_of_birth, &last_id], + "INSERT INTO cats (name, color_id) values (?1, ?2)", + &[&cat.to_string(), &last_id], )?; } } - let mut stmt = conn.prepare("SELECT c.name, date_of_birth, cc.name from cats c + let mut stmt = conn.prepare("SELECT c.name, cc.name from cats c INNER JOIN cat_colors cc ON cc.id = c.color_id;")?; - let cats = stmt.query_map(&[], |row| { - Cat { - name: row.get(0), - date_of_birth: row.get(1), - color: row.get(2) - } - })?; + + let cats = stmt + .query_map(NO_PARAMS, |row| + Ok( + Cat { + name: row.get(0)?, + color: row.get(1)?, + } + ) + )?; for cat in cats { println!("Found cat {:?}", cat);