gpt4 book ai didi

mysql::value::FromValue 在调用 mysql::from_row 时没有为大元组实现

转载 作者:行者123 更新时间:2023-11-29 08:29:38 26 4
gpt4 key购买 nike

我想转换结构 User 以添加十个字母命名的属性,从 afmp

#[macro_use]
extern crate mysql;

use mysql::Pool;

#[derive(Debug, Default)]
pub struct User {
/// The ID number of the user.
pub id: u32,
/// The name of the user / machine.
pub user: String,

/// The name of the company the user is attached to.
pub company: Option<String>,
/// The mass of the weight in the machine.
pub mass_of_weight: Option<f64>,
/// The total height of the machine.
pub total_height: Option<f64>,

/// The callibration coefficient for BMWI calculations involving this machine.
pub k_coefficient: Option<f64>,
/// The EU value for the machine at this point in time.
pub energy_utilisation: Option<f64>,
pub a: Option<f64>,
pub b: Option<f64>,
pub c: Option<f64>,
pub d: Option<f64>,
pub e: Option<f64>,
pub f: Option<f64>,
pub m: Option<f64>,
pub n: Option<f64>,
pub o: Option<f64>,
pub p: Option<f64>,
}

impl User {
/// Selects a user from the database, given their id.
pub fn new(id: u32, pool: &Pool) -> Option<Self> {
let (
user,
company,
mass_of_weight,
total_height,
k_coefficient,
energy_utilisation,
a,
b,
c,
d,
e,
f,
m,
n,
o,
p,
) = match pool.prep_exec(
include_str!("../../resources/sql/users/select.mysql"),
params!{"id" => &id},
).unwrap()
.next()
{
Some(Ok(row)) => mysql::from_row(row),
_ => return None,
};

Some(User {
id: id,
user: user,
company: company,
mass_of_weight: mass_of_weight,
total_height: total_height,
k_coefficient: k_coefficient,
energy_utilisation: energy_utilisation,
a: a,
b: b,
c: c,
d: d,
e: e,
f: f,
m: m,
n: n,
o: o,
p: p,
})
}
}

fn main() {}

当我运行 cargo build 时,我得到这个错误:

error[E0277]: the trait bound `(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _): mysql::prelude::FromValue` is not satisfied
--> src/main.rs:62:30
|
62 | Some(Ok(row)) => mysql::from_row(row),
| ^^^^^^^^^^^^^^^ the trait `mysql::prelude::FromValue` is not implemented for `(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _)`
|
= note: required because of the requirements on the impl of `mysql::prelude::FromRow` for `(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _)`
= note: required by `mysql::from_row`

SQL 是:

SELECT id, user, company, mass_of_weight, total_height, k_coefficient,
energy_utilisation, a, b, c, d, e, f, m, n, o, p
FROM users

我该如何处理这个错误?我试过 cargo clean;它没有帮助。

最佳答案

编译器没有骗你。 FromRow仅针对特定大小的元组实现。 author of the library suggests calling Row::take :

pub fn new(id: u32, pool: &Pool) -> Option<Self> {
let sql = include_str!("../../resources/sql/users/select.mysql");

let mut row = match pool.prep_exec(sql, params!{"id" => &id}).unwrap().next() {
Some(Ok(row)) => row,
_ => return None,
};

Some(User {
id: row.take("id").unwrap(),
user: row.take("user").unwrap(),
company: row.take("company").unwrap(),
mass_of_weight: row.take("mass_of_weight").unwrap(),
total_height: row.take("total_height").unwrap(),
k_coefficient: row.take("k_coefficient").unwrap(),
energy_utilisation: row.take("energy_utilisation").unwrap(),
a: row.take("a").unwrap(),
b: row.take("b").unwrap(),
c: row.take("c").unwrap(),
d: row.take("d").unwrap(),
e: row.take("e").unwrap(),
f: row.take("f").unwrap(),
m: row.take("m").unwrap(),
n: row.take("n").unwrap(),
o: row.take("o").unwrap(),
p: row.take("p").unwrap(),
})
}

由于您没有对数据执行任何转换,您可能想看看 Diesel更适合您的应用。

关于mysql::value::FromValue 在调用 mysql::from_row 时没有为大元组实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49443070/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com