如何应要求将后Gresql数字类型解析为生锈的数字类型?

发布于 2025-02-06 16:44:09 字数 1752 浏览 1 评论 0原文

相关代码是这样:

use chrono::{DateTime, Utc}; // 0.4.10
use postgres::{Client, NoTls, Config, types::{FromSql, Type}};
//...
for row in client.query("SELECT * FROM pricetable;", &[])? {
    let time:DateTime<Utc> = row.get(0);
    let price:i64      = row.get(1);

    println!("found datum:  {:?}", &row);
    println!("found datum: {} {} ", time, price);
}

运行时我遇到的错误是:

thread 'main' panicked at 'error retrieving column 1: error

deserializing column 1: cannot convert between the Rust type `i64` 

and the Postgres type `numeric`', /home/me/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-postgres-0.7.6/src/row.rs:151:25

其中Pricenumeric在SQL类型方面键入。我似乎无法将其解析为任何标准的生锈类型,例如u64i64等。鉴于它们似乎无法实现fromsql 特征 postgres 板条板出口。 (我意识到这清楚地表明在这里,但这不是我的错误。 0_4“]}

我已经看到人们建议声明自己的包装器类型,例如struct float64(u64) 和实现 tosql fromsql 上面。是否有参考实现?

Relevant code is this:

use chrono::{DateTime, Utc}; // 0.4.10
use postgres::{Client, NoTls, Config, types::{FromSql, Type}};
//...
for row in client.query("SELECT * FROM pricetable;", &[])? {
    let time:DateTime<Utc> = row.get(0);
    let price:i64      = row.get(1);

    println!("found datum:  {:?}", &row);
    println!("found datum: {} {} ", time, price);
}

The error I'm getting when I run this is thus:

thread 'main' panicked at 'error retrieving column 1: error

deserializing column 1: cannot convert between the Rust type `i64` 

and the Postgres type `numeric`', /home/me/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-postgres-0.7.6/src/row.rs:151:25

where price is a NUMERIC type in terms of SQL types. I can't seem to parse it into any of the standard rust types like u64,i64 etc. given that they don't seem to implement FromSql trait that the postgres crate exports. (I realize it's stated plainly here that they it is, but that's not what my error says. Do i need to enable some feature? Cargo.toml has postgres={version="0.19.3",features= ["with-chrono-0_4"]})

I've seen people recommend declaring your own wrapper type like struct Float64(u64) and implementing ToSql and FromSql on those.. Is that the only way to get data out of Postgres into Rust? Is there a reference implementation if so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小草泠泠 2025-02-13 16:44:09

使用 DECIMAL 代替本机数据类型:它Incements FromsqlTOSQL实际上未针对常规Rust的数值类型和SQL的数字实现。这是由于数字是任意的精度。

Use something like decimal instead of the native data types: it impements FromSql and ToSql which are not in fact implemented for regular rust's numerical types and SQL's NUMERIC. This is due to NUMERIC being of arbitrary precision.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文