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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With