Hello All,
SuperRustNoob here. I have been busy recreating simple programs I originally created in C now in Rust and have gotten stuck on trying to extract the data from from a SQL query so that I can format it to print to my console.
I have a very simple sqlite3 database with 50 records in 1 table that have basic info on US states. All of the data is in string/text format (for number values, I deal with that after it is in my program). My code is a modified version from the rusqlite documentation here: rusqlite - Rust
My code:
use rusqlite::{Connection, Result, NO_PARAMS};
#[derive(Debug)]
struct State {
key: String,
name: String,
capital: String,
population: String,
area: String,
date: String,
bird: String,
}
fn main() -> Result<()> {
let conn = Connection::open("StateInfo.sqlite3")?;
let mut stmt = conn
.prepare("SELECT key, name, capital, population, area, date, bird FROM Table_Main")?;
let state_iter = stmt
.query_map(NO_PARAMS, |row| Ok(State {
key: row.get(0)?,
name: row.get(1)?,
capital: row.get(2)?,
population: row.get(3)?,
area: row.get(4)?,
date: row.get(5)?,
bird: row.get(6)?,
}))?;
for state in state_iter {
println!("{:?}", state.unwrap());
}
Ok(())
}
When I run the program, I get the result shown below. All of the data is there. My question is how do I extract it from how it is being held? That is, I want to be able to run a for loop to print out all of the data in an order such as this:
for i in 0..numrecords {
println!("{}\t {}\t {}\t", state[i].name, state[i].population, state[i].bird);
}
I am stuck on how to get the data out where I can use it.
Sorry for the stupid question
Thanks,
Jerry