S
S
Somewhere Intech2019-04-05 22:00:55
SQLite
Somewhere Intech, 2019-04-05 22:00:55

How to properly handle rusqlite mappedrows?

Made by example , but swears at data types, why?

#[derive(Debug)]
pub struct Candle{
  pub timestamp: i32,
  pub open: f64,
  pub close:f64,
  pub min: f64,
  pub max: f64,
  pub ticks: Vec<CandleTick>
}
fn read()->Result<Vec<Candle>>{
  let conn = Connection::open("candles.db")?;
  let mut stmt = conn.prepare("SELECT * from candles limit 10")?;
    let res :Vec<Candle>= stmt.query_map(&[], |row| {
        Candle {
            timestamp: row.get(1).unwrap(),
            open: row.get(2).unwrap(),
            close: row.get(3).unwrap(),
            min: row.get(4).unwrap(),
            max: row.get(5).unwrap(),
            ticks: Vec::new()
        }
    })?;
    Ok(res)
}

unwrap also added because of the abuse that Result should be
In general, the compiler's answer:
error[E0308]: mismatched types
  --> src/lib.rs:37:9
   |
37 | /         Candle {
38 | |             timestamp: row.get(1).unwrap(),
39 | |             open: row.get(2).unwrap(),
40 | |             close: row.get(3).unwrap(),
...  |
43 | |             ticks: Vec::new()
44 | |         }
   | |_________^ expected enum `std::result::Result`, found struct `Candle`
   |
   = note: expected type `std::result::Result<_, rusqlite::error::Error>`
              found type `Candle`

error[E0308]: try expression alternatives have incompatible types
  --> src/lib.rs:36:27
   |
36 |       let res :Vec<Candle>= stmt.query_map(&[], |row| {
   |  ___________________________^
37 | |         Candle {
38 | |             timestamp: row.get(1).unwrap(),
39 | |             open: row.get(2).unwrap(),
...  |
44 | |         }
45 | |     })?;
   | |_______^ expected struct `std::vec::Vec`, found struct `rusqlite::row::MappedRows`
   |
   = note: expected type `std::vec::Vec<Candle>`
              found type `rusqlite::row::MappedRows<'_, [[email protected]/lib.rs:36:47: 45:6]>`

Type for res designated "at random" of course..

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
T
Tyranron, 2019-04-05
@john36allTa

The tutorial is outdated. It's best to always read the official doc . It is very convenient in Rust and it is customary in the community to flavor it well with examples.
The first error says that you return Candle {...}, while expected Result(this is just an outdated part of the tutorial). Just do Ok(Candle {...})it (the example from the documentation shows just the same).
The second error says that you are trying to rusqlite::row::MappedRowssubstitute the return type where you explicitly expect Vec. Rust is a statically and strongly typed language, that's not how it works. In fact, you just need to add .collect()after ?. Since it MappedRowsimplementsIterator , it .collect()will just run through all the iterator values ​​and collect them intoVecwhich is what you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question