如何应要求将后Gresql数字类型解析为生锈的数字类型?
相关代码是这样:
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
其中Price
是numeric
在SQL类型方面键入。我似乎无法将其解析为任何标准的生锈类型,例如u64
,i64
等。鉴于它们似乎无法实现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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
DECIMAL
代替本机数据类型:它IncementsFromsql
和TOSQL
实际上未针对常规Rust的数值类型和SQL的数字
实现。这是由于数字
是任意的精度。Use something like
decimal
instead of the native data types: it impementsFromSql
andToSql
which are not in fact implemented for regular rust's numerical types and SQL'sNUMERIC
. This is due toNUMERIC
being of arbitrary precision.