rustrust-chronorusqlite

How to insert and fetch date in a sqlite database using rusqlite?


I have a struct with NaiveDate field and I want to insert this field to a table. This is my whole code

use chrono::naive::NaiveDate; 
use rusqlite::{params, Connection, Result};

#[derive(Debug)]
struct Person {
    id: i32,
    name: String,
    date: NaiveDate,
}

fn main()->Result<()>{
    let date_str = "2020-04-12";
    let naive_date = NaiveDate::parse_from_str(date_str, "%Y-%m-%d").unwrap();

    let me = Person {
        id: 0,
        name: "Steven".to_string(),
        date: naive_date,
    };
    println!("{:?}",me);
    
    let conn = Connection::open_in_memory()?;
    
        
    conn.execute(
        "CREATE TABLE person (
                  id              INTEGER PRIMARY KEY,
                  name            TEXT NOT NULL,
                  date            TEXT
                  )",
        [],
    )?;
    
    conn.execute(
    "INSERT INTO person (name, date) VALUES (?1, ?2)",
        params![me.name, me.date],
    )?;
    
    let mut stmt = conn.prepare("SELECT id, name, date FROM person")?;
    let person_iter = stmt.query_map([], |row| {
        Ok(Person {
            id: row.get(0)?,
            name: row.get(1)?,
            date: row.get(2)?,
        })
    })?;

    for person in person_iter {
        println!("Found person {:?}", person.unwrap());
    }
    
    Ok(())
}

But it gives these two errors

|   the trait `FromSql` is not implemented for `NaiveDate`
|   the trait `ToSql` is not implemented for `NaiveDate`

cargo.toml

chrono = "0.4.19"
rusqlite = "0.25.0"

Solution

  • You must compile with the additional chrono feature to have the FromSql and ToSql traits implemented for chrono types.

    Change the declaration in your Cargo.toml to be

    chrono = "0.4.19"
    rusqlite = { version = "0.25.0", features = ["chrono"] }