#Sqlite fails on unwrap

10 messages · Page 1 of 1 (latest)

indigo verge
#

So I have this code:

pub fn def_nutrients() {
    let connection = sqlite::open("food.db").unwrap();

    let mut cursor = connection
        .prepare("SELECT * FROM foodList limit 100")
        .unwrap()
        .into_cursor();

    while let Some(row) = cursor.next().unwrap() {
        let temporary_food_struct = FoodStruct {
            name: row[0].as_string().unwrap().to_string(),
            author: row[1].as_string().unwrap().to_string(),
            description: row[2].as_string().unwrap().to_string(),
            ingredients: row[11].as_string().unwrap().to_string(),
            method: row[12].as_string().unwrap().to_string(),
            difficulty: row[13].as_string().unwrap().to_string(),
            img_url: row[15].as_string().unwrap().to_string(),
            servings: row[14].as_integer().unwrap() as u16,
            kcal: row[3].as_integer().unwrap_or(0) as u16,
            fat: row[4].as_integer().unwrap_or(0) as u16,
            saturates: row[5].as_integer().unwrap_or(0) as u16,
            carbs: row[6].as_integer().unwrap_or(0) as u16,
            sugars: row[7].as_integer().unwrap_or(0) as u16,
            fibre: row[8].as_integer().unwrap_or(0) as u16,
            protein: row[9].as_integer().unwrap_or(0) as u16,
            salt: row[10].as_integer().unwrap_or(0) as u16,
        };

        println!("{:?}", temporary_food_struct);
    }
}```
For some reason, using unwrap instead of unwrap_or gives me this error:
```thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/lib.rs:105:39```
Even though my database does have the values. Any idea why this might be happening?
gusty steeple
#

first of all

#

you wanna use this if some of your fields can be null

#

it's implemented on the rusqlite result
you just have to bring it into scope

indigo verge
#

Thank you

#

I solved the issue, I just needed to use as_float instead of as_integer

gusty steeple
#

can you go ahead and archive the thread?

#

@indigo verge

indigo verge
#

not sure how to do that