如何在 Rust 中从 MySQL 获取二进制列? [英] How do I fetch binary columns from MySQL in Rust?

查看:40
本文介绍了如何在 Rust 中从 MySQL 获取二进制列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 https://docs.rs/mysql/19.0.1/mysql/ 从 mySQL 数据库中获取一些行.我将它们分配给这样的结构:

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)
}

问题是我的某些行以 mysql binary 格式存储(例如 uuid binary(16).)我发现了 mysql::Binary 但是当我想在我的结构中使用它时,我收到以下错误:

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)

和一些类似的带有Eq"的等

and a few similar ones with "Eq" etc.

我的问题是:如何使用 rust mysql-package 获取二进制列?我无法使用提供的文档找到任何内容.任何例子都会很棒.

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.

推荐答案

也许你可以将你的数据作为一个 vec 的字节来读取,如下所示

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

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

这篇关于如何在 Rust 中从 MySQL 获取二进制列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆