How do I fetch binary columns from MySQL in Rust?

旧街凉风 提交于 2021-01-27 06:31:10

问题


I am using https://docs.rs/mysql/19.0.1/mysql/ to fetch some rows from a mySQL-Database. I assign them to a struct like this:

use mysql::*;
use mysql::prelude::*;
use serde::Serialize;
 
#[derive(Debug, PartialEq, Eq, Serialize)]
pub struct Policy {
    sub: Option<mysql::Binary>,
    contents: Option<String>,
}
 
pub fn list_policies() -> Result<Vec<Policy>> {
    let url = "";
    let pool = Pool::new(url)?;
    let mut connection = pool.get_conn()?;
 
    let policies: Vec<Policy> = connection.query_map("SELECT sub, contents FROM policy", |(sub, contents)| {
        Policy { sub, contents }
    },)?;
 
    Ok(policies)
}

Problem is that some of my rows are stored in mysql binary format (uuid binary(16) for example.) I discovered the mysql::Binary but when I want to use it in my struct, I get the following error:

error[E0369]: binary operation `==` cannot be applied to type `std::option::Option<mysql::Binary>`
 --> src/database/mod.rs:7:2
  |
7 |     sub: Option<mysql::Binary>,
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

and a few similar ones with "Eq" etc.

My question is: How can I fetch binary-columns with the rust mysql-package? I can't find anything using the provided documentation. Any example would be great.


回答1:


Maybe you can read your data as a vec of bytes like the following

pub struct Policy {
    sub: Option<Vec<u8>>,
    contents: Option<String>,
}


来源:https://stackoverflow.com/questions/64076722/how-do-i-fetch-binary-columns-from-mysql-in-rust

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!