I'm trying to get a value for a query, but this value can be NULL and I don't know how to handle it in Rust. Here is my code:
let stmt = conn.prepare("SELECT * FROM pictures").unwrap();
for row in stmt.query(&[]).unwrap() {
let id: i32 = row.get("id");
let author: String = row.get("author");
let description: String = row.get("description");
let rating: String = row.get("rating");
let gps_lat: String = row.get("gps_lat");
let gps_long: String = row.get("gps_long");
let date_taken: chrono::NaiveDate = row.get("date_taken");
println!("id -> {}
author -> {}
description -> {}
rating -> {}
gps_lat -> {}
gps_long -> {}
date -> {}
", id, author, description, rating, gps_lat, gps_long, date_taken);
}
When I execute the code, the first picture comes well because the rating column isn't NULL. But the second picture fails and gives me "Conversion(WasNull)", because there is not rating and I try to convert a NULL into a chrono::NaiveDate.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…