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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql :: value :: FromValue在调用mysql :: from_row时没有为大元组实现相关的知识,希望对你有一定的参考价值。

我想转换结构Usera添加十个字母命名属性到fmp

#[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时没有为大元组实现的主要内容,如果未能解决你的问题,请参考以下文章

mysql中如何将函数结果作为values值插入?

mysql特征缩放计算

mysql 流程函数

MySQL 运算符

MySQL插入数据

MySQL INSERT命令