mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-21 19:13:07 +00:00
Fix sqlite example formatting and add syntax highlighting (#533)
* Fix sqlite example formatting and add syntax highlighting * Format sqlite insert_select example using rustfmt * Improve sqlite example code readability while adhering to rustfmt
This commit is contained in:
parent
a38c7cb59f
commit
dddb1a9fa5
1 changed files with 25 additions and 27 deletions
|
@ -5,61 +5,59 @@
|
|||
[`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,no_run
|
||||
extern crate rusqlite;
|
||||
|
||||
use rusqlite::{Connection, Result};
|
||||
use rusqlite::NO_PARAMS;
|
||||
use rusqlite::{Connection, Result};
|
||||
use std::collections::HashMap;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Cat {
|
||||
name: String,
|
||||
color: String
|
||||
color: String,
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
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{
|
||||
for (color, catnames) in &cat_colors {
|
||||
conn.execute(
|
||||
"INSERT INTO cat_colors (name) values (?1)",
|
||||
&[&color.to_string()],
|
||||
)?;
|
||||
let last_id : String = conn.last_insert_rowid().to_string();
|
||||
let last_id: String = conn.last_insert_rowid().to_string();
|
||||
|
||||
for cat in catnames{
|
||||
conn.execute(
|
||||
"INSERT INTO cats (name, color_id) values (?1, ?2)",
|
||||
&[&cat.to_string(), &last_id],
|
||||
)?;
|
||||
for cat in catnames {
|
||||
conn.execute(
|
||||
"INSERT INTO cats (name, color_id) values (?1, ?2)",
|
||||
&[&cat.to_string(), &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(NO_PARAMS, |row|
|
||||
Ok(
|
||||
Cat {
|
||||
name: row.get(0)?,
|
||||
color: row.get(1)?,
|
||||
}
|
||||
)
|
||||
)?;
|
||||
|
||||
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(NO_PARAMS, |row| {
|
||||
Ok(Cat {
|
||||
name: row.get(0)?,
|
||||
color: row.get(1)?,
|
||||
})
|
||||
})?;
|
||||
|
||||
for cat in cats {
|
||||
println!("Found cat {:?}", cat);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
[`Connection::open`]: https://docs.rs/rusqlite/*/rusqlite/struct.Connection.html#method.open
|
||||
|
|
Loading…
Reference in a new issue