The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Query A DATETIME Field In MySQL With Rust's Diesel Crate

TODO: Pull subtitle into page object

These are scratch notes with code snippets showing what I did to get a datetime working. The model is up top with the other stuff below.

Code
use chrono::NaiveDateTime;
use diesel::prelude::*;

#[derive(Queryable, Debug)]
pub struct TestItem2 {
    pub id: i32,
    pub data: Option<NaiveDateTime>,
}

chrono crate

The _chrono__ crate is used behind the scenes. It has to be added to _Cargo.toml__ and the feature turned on for _diesel__. Mine looks like this:

Code
[dependencies]
chrono = "0.4.24"
diesel = { version = "2.0.0", features = ["mysql", "serde_json", "chrono"] }
dotenvy = "0.15"

Other Stuff

This is the other stuff that uses the code

Code
CREATE TABLE test2 (
  id INT PRIMARY KEY,
  datetime DATETIME
)
Code
use bluesky_feed_sorter::database::establish_connection;
use bluesky_feed_sorter::models::*;
use bluesky_feed_sorter::schema::test2::dsl::*;
use diesel::prelude::*;

fn main() {
    println!("Connecting");
    let conn = &mut establish_connection();
    let results = test2.load::<TestItem2>(conn).expect("Error loading posts");
    for result in results {
        dbg!(result);
    }
}

Code
use diesel::prelude::*;
use dotenvy::dotenv;
use std::env;

pub fn establish_connection() -> MysqlConnection {
    dotenv().ok();
    let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
    MysqlConnection::establish(&database_url)
        .unwrap_or_else(|_| panic!("Error connecting to database"))
}

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Query A DATETIME Field In MySQL With Rust's Diesel Crate

-- note

These are scratch notes with code snippets showing
what I did to get a datetime working. The model is
up top with the other stuff below.


-- code
-- rust

use chrono::NaiveDateTime;
use diesel::prelude::*;

#[derive(Queryable, Debug)]
pub struct TestItem2 {
    pub id: i32,
    pub data: Option<NaiveDateTime>,
}


-- h3

chrono crate

The _chrono__ crate is used behind the scenes. It 
has to be added to _Cargo.toml__ and the feature
turned on for _diesel__. Mine looks like this:


-- code
-- toml


[dependencies]
chrono = "0.4.24"
diesel = { version = "2.0.0", features = ["mysql", "serde_json", "chrono"] }
dotenvy = "0.15"


-- h3

Other Stuff

This is the other stuff that uses the code


-- code
-- sql 

CREATE TABLE test2 (
  id INT PRIMARY KEY,
  datetime DATETIME
)


-- code
-- rust


use bluesky_feed_sorter::database::establish_connection;
use bluesky_feed_sorter::models::*;
use bluesky_feed_sorter::schema::test2::dsl::*;
use diesel::prelude::*;

fn main() {
    println!("Connecting");
    let conn = &mut establish_connection();
    let results = test2.load::<TestItem2>(conn).expect("Error loading posts");
    for result in results {
        dbg!(result);
    }
}



-- code
-- rust

use diesel::prelude::*;
use dotenvy::dotenv;
use std::env;

pub fn establish_connection() -> MysqlConnection {
    dotenv().ok();
    let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
    MysqlConnection::establish(&database_url)
        .unwrap_or_else(|_| panic!("Error connecting to database"))
}


-- ref
-- url: https://docs.diesel.rs/2.0.x/chrono/naive/struct.NaiveDateTime.html

-- ref
-- url: https://docs.diesel.rs/2.0.x/chrono/index.html

-- ref
-- url: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-types.html




-- categories
-- Rust
-- Diesel 
-- MySQL 

-- metadata
-- date: 2023-05-25 21:14:23
-- id: 2qjfedbs
-- site: aws
-- type: post
-- status: draft