前提・実現したいこと
下記のMySQLのテーブルをRustで表示させたい。
mysql> show columns from memos; +---------+-----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+-----------+------+-----+---------+----------------+ | id | mediumint | NO | PRI | NULL | auto_increment | | name | char(30) | YES | | NULL | | | comment | char(50) | YES | | NULL | | | time | datetime | YES | | NULL | | +---------+-----------+------+-----+---------+----------------+
データは次のとおりです。
mysql> select * from memos; +----+----------+---------+---------------------+ | id | name | comment | time | +----+----------+---------+---------------------+ | 6 | taro | kuri | 2021-04-14 12:25:17 | | 7 | Susie | ringo | 2021-04-14 09:43:39 | | 8 | ito | food | 2021-04-14 09:43:20 | | 9 | momotaro | momo | 2021-04-14 12:24:48 | +----+----------+---------+---------------------+
発生している問題・エラーメッセージ
下記のように数値や文字列までは表示できました。
apo@gentoo ~/Documents/project/make/rust/mysql_chrone/sample_proj $ cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.02s Running `target/debug/sample_proj` *** 開始 *** 6 taro kuri 7 Susie ringo 8 ito food 9 momotaro momo *** 終了 ***
ですが日付(DATETIME)が表示できません。
文字列として表示させようとすると、コンパイルは通るのですが下記ようなエラーがでます。
apo@gentoo ~/Documents/project/make/rust/mysql/sample_proj $ cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.02s Running `target/debug/sample_proj` *** 開始 *** thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: DeserializationError(Utf8Error { valid_up_to: 0, error_len: Some(1) })', src/main.rs:36:107 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
該当のソースコード
Rust
1use crate::utils::establish_connection; 2 3use diesel::deserialize::QueryableByName; 4use diesel::mysql::MysqlConnection; 5use diesel::prelude::*; 6use diesel::sql_query; 7 8mod utils; 9 10type DB = diesel::mysql::Mysql; 11 12#[derive(Debug)] 13pub struct Memos { 14 id: i32, 15 name: String, 16 comment: String, 17 time: String, 18} 19 20impl QueryableByName<DB> for Memos { 21 fn build<R: diesel::row::NamedRow<diesel::mysql::Mysql>>(row: &R,) -> diesel::deserialize::Result<Self> { 22 Ok(Memos { 23 id: row.get("id")?, 24 name: row.get("name")?, 25 comment: row.get("comment")?, 26 time: row.get("time")?, 27 } 28 ) 29 } 30} 31 32fn simple_sql() { 33 34 let connection: MysqlConnection = establish_connection(); 35 36 let memos: Vec<Memos> = sql_query("SELECT id, name, comment, time FROM memos",).load(&connection).unwrap(); 37 38 for uu in memos.iter(){ 39 println!("{}\t{}\t{}\t{}", uu.id, uu.name, uu.comment, uu.time); 40 //println!("{}\t{}\t{}", uu.id, uu.name, uu.comment); 41 } 42} 43 44fn main (){ 45 eprintln! ("*** 開始 ***"); 46 simple_sql(); 47 eprintln! ("*** 終了 ***"); 48}
試したこと
Rustで日時を扱うという記事を見て下記のように変更してみましたが、今度はコンパイルできませんでした。
./Cargo.toml
[package] name = "sample_proj" version = "0.1.0" edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diesel = { version = "*", features = ["mysql"] } chrono = "0.4"
./src/main.rs
use crate::utils::establish_connection; use diesel::deserialize::QueryableByName; use diesel::mysql::MysqlConnection; use diesel::prelude::*; use diesel::sql_query; use chrono::{Utc, Local, DateTime, Date}; mod utils; type DB = diesel::mysql::Mysql; #[derive(Debug)] pub struct Memos { id: i32, name: String, comment: String, //time: String, time: DateTime<Utc>, } impl QueryableByName<DB> for Memos { fn build<R: diesel::row::NamedRow<diesel::mysql::Mysql>>(row: &R,) -> diesel::deserialize::Result<Self> { Ok(Memos { id: row.get("id")?, name: row.get("name")?, comment: row.get("comment")?, time: row.get("time")?, } ) } } fn simple_sql() { let connection: MysqlConnection = establish_connection(); let memos: Vec<Memos> = sql_query("SELECT id, name, comment, time FROM memos",).load(&connection).unwrap(); for uu in memos.iter(){ println!("{}\t{}\t{}\t{}", uu.id, uu.name, uu.comment, uu.time); //println!("{}\t{}\t{}", uu.id, uu.name, uu.comment); } } fn main (){ eprintln! ("*** 開始 ***"); simple_sql(); eprintln! ("*** 終了 ***"); }
コンパイル → エラー
apo@gentoo ~/Documents/project/make/rust/mysql_chrono/sample_proj $ cargo build Compiling sample_proj v0.1.0 (/home/apo/Documents/project/make/rust/mysql_chrono/sample_proj) warning: unused imports: `Date`, `Local` --> src/main.rs:8:19 | 8 | use chrono::{Utc, Local, DateTime, Date}; | ^^^^^ ^^^^ | = note: `#[warn(unused_imports)]` on by default error[E0277]: the trait bound `chrono::DateTime<Utc>: FromSql<_, Mysql>` is not satisfied --> src/main.rs:30:39 | 30 | time: row.get("time")?, | ^^^ the trait `FromSql<_, Mysql>` is not implemented for `chrono::DateTime<Utc>` error: aborting due to previous error; 1 warning emitted For more information about this error, try `rustc --explain E0277`. error: could not compile `sample_proj` To learn more, run the command again with --verbose. apo@gentoo ~/Documents/project/make/rust/mysql_chrono/sample_proj $ cargo build --verbose Fresh autocfg v1.0.1 Fresh tinyvec_macros v0.1.0 Fresh unicode-xid v0.2.1 Fresh matches v0.1.8 Fresh pkg-config v0.3.19 Fresh percent-encoding v1.0.1 Fresh byteorder v1.4.3 Fresh tinyvec v1.2.0 Fresh unicode-bidi v0.3.5 Fresh proc-macro2 v1.0.26 Fresh unicode-normalization v0.1.17 Fresh libc v0.2.93 Fresh num-traits v0.2.14 Fresh quote v1.0.9 Fresh idna v0.1.5 Fresh time v0.1.44 Fresh syn v1.0.69 Fresh url v1.7.2 Fresh mysqlclient-sys v0.2.4 Fresh num-integer v0.1.44 Fresh diesel_derives v1.4.1 Fresh diesel v1.4.6 Fresh chrono v0.4.19 Compiling sample_proj v0.1.0 (/home/apo/Documents/project/make/rust/mysql_chrono/sample_proj) Running `rustc --crate-name sample_proj --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=5109028e9619baef -C extra-filename=-5109028e9619baef --out-dir /home/apo/Documents/project/make/rust/mysql_chrono/sample_proj/target/debug/deps -C incremental=/home/apo/Documents/project/make/rust/mysql_chrono/sample_proj/target/debug/incremental -L dependency=/home/apo/Documents/project/make/rust/mysql_chrono/sample_proj/target/debug/deps --extern chrono=/home/apo/Documents/project/make/rust/mysql_chrono/sample_proj/target/debug/deps/libchrono-e93bd251cf4961dc.rlib --extern diesel=/home/apo/Documents/project/make/rust/mysql_chrono/sample_proj/target/debug/deps/libdiesel-8bb2074f68278926.rlib -L native=/usr/lib64` warning: unused imports: `Date`, `Local` --> src/main.rs:8:19 | 8 | use chrono::{Utc, Local, DateTime, Date}; | ^^^^^ ^^^^ | = note: `#[warn(unused_imports)]` on by default error[E0277]: the trait bound `chrono::DateTime<Utc>: FromSql<_, Mysql>` is not satisfied --> src/main.rs:30:39 | 30 | time: row.get("time")?, | ^^^ the trait `FromSql<_, Mysql>` is not implemented for `chrono::DateTime<Utc>` error: aborting due to previous error; 1 warning emitted For more information about this error, try `rustc --explain E0277`. error: could not compile `sample_proj` Caused by: process didn't exit successfully: `rustc --crate-name sample_proj --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=5109028e9619baef -C extra-filename=-5109028e9619baef --out-dir /home/apo/Documents/project/make/rust/mysql_chrono/sample_proj/target/debug/deps -C incremental=/home/apo/Documents/project/make/rust/mysql_chrono/sample_proj/target/debug/incremental -L dependency=/home/apo/Documents/project/make/rust/mysql_chrono/sample_proj/target/debug/deps --extern chrono=/home/apo/Documents/project/make/rust/mysql_chrono/sample_proj/target/debug/deps/libchrono-e93bd251cf4961dc.rlib --extern diesel=/home/apo/Documents/project/make/rust/mysql_chrono/sample_proj/target/debug/deps/libdiesel-8bb2074f68278926.rlib -L native=/usr/lib64` (exit status: 1)
mysql - Dieselを使用してmySQLデータベースから日時を取得するのサイトのコードも試してみたのですが、コンパイルできませんでした。
補足情報(FW/ツールのバージョンなど)
OS:Gentoo
Cargo:cargo 1.53.0-nightly (65d57e6f3 2021-04-04)
MySQL:mysql Ver 8.0.23 for Linux on x86_64 (Gentoo Linux mysql-8.0.23)
全てのソースコード
String ver
chono ver
ご教示よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/16 05:34