Postgresql 在rust postgres中使用固定精度数字的正确方法是什么?
定义固定精度数字(Postgres中的十进制或数字)并将其传递给rust Postgres中的insert的正确方法是什么Postgresql 在rust postgres中使用固定精度数字的正确方法是什么?,postgresql,rust,Postgresql,Rust,定义固定精度数字(Postgres中的十进制或数字)并将其传递给rust Postgres中的insert的正确方法是什么 transaction.execute("INSERT INTO commons_account( display_name, blocked, balance, blocked_balance, password, salt) VALUES ($1, $2, $3, $4, $5, $6);", &[&"bo
transaction.execute("INSERT INTO commons_account(
display_name, blocked, balance, blocked_balance, password, salt)
VALUES ($1, $2, $3, $4, $5, $6);", &[&"bob", &false, &0, &0, &"dfasdfsa", &"dfsdsa"]).unwrap();
余额和冻结余额都是数值,运行此代码会导致此错误
thread 'test' panicked at 'called `Result::unwrap()` on an `Err` value: WrongType(Numeric)'
Numeric
不在列表中。
您需要自己通过以下方式实现trait-ToSql:
struct Float64(f64);
impl ToSql for Float64 { // Or a fixed-precision type.
to_sql_checked!();
fn to_sql<W: Write + ?Sized>(&self, _: &Type, mut w: &mut W, _: &SessionInfo) -> Result<IsNull> {
let num = 0;
w.write_f64::<BigEndian>(num)?;
*self = Float64(num);
Ok(IsNull::No)
}
accepts!(Type::Numeric);
}
structfloat64(f64);
浮点64{//或固定精度类型的impl ToSql。
要检查sql!();
fn to_sql(&self,u:&Type,mut w:&mut w,u:&SessionInfo)->结果{
设num=0;
w、 写入64::(num)?;
*self=64(num);
确定(IsNull::否)
}
接受!(类型::数字);
}
我刚刚发现Rust没有内置的十进制类型,这让一切变得更加复杂difficult@ibrabeicker有,;另见