2015-12-15 41 views
2

定义一个固定的精确数字(Postgres中的十进制或数字)并将其传递给rust-postgres中的插入的正确方法是什么?在rust-postgres中使用固定精确数字的正确方法是什么?

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(); 

两个平衡和阻止平衡是numeric并运行该代码给出了这样的错误

thread 'test' panicked at 'called `Result::unwrap()` on an `Err` value: WrongType(Numeric)' 

回答

2

Numeric不在supported types。 您需要实现性状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; 
     try!(w.write_f64::<BigEndian>(num)); 
     *self = Float64(num); 
     Ok(IsNull::No) 
    } 

    accepts!(Type::Numeric); 
} 
+0

我刚刚发现,锈不具有一个内置的十进制类型,这只是做的一切更加困难 – ibrabeicker

+0

@ibrabeicker,有https://开头WWW .reddit.com/R /防锈/评论/ 3wfhro/decimal_d128_type_for_rust_for_decimal_floating /;另见https://www.reddit.com/r/rust/comments/3wubj0/numeric_types_in_rust/ – ArtemGr

相关问题