PostgreSQL在查询返回空的情况下连续返回NULL

时间:2019-07-18 05:54:23

标签: sql postgresql rust

我有一个通用代码,试图在查询返回后访问字段值。

SELECT 
    ROUND(AVG(EXTRACT(EPOCH FROM (time::timestamp)))) as extended_time
FROM log_info 
WHERE 
    id = 1 AND 
    code = 200;

但是,这将返回NULL值作为单行,这导致我的应用程序崩溃。

for row in &con.query(query, args.as_slice()).unwrap() {
    let extended_time: i32 = row.get("extended_time");
    ...
}

崩溃并显示以下错误:-

error retrieving column "extended_time": Conversion(WasNull)

手动运行会返回

 extended_time
-------------

(1 row)

没有AVG查询将返回预期结果

SELECT 
    EXTRACT(EPOCH FROM (time::timestamp)) as extended_time
FROM log_info 
WHERE 
    id = 1 AND 
    code = 200;

 extended_time
-------------
(0 rows)

1 个答案:

答案 0 :(得分:0)

i32不能为空。您应该使用Option,因为查询可以返回NULL

let extended_time: Option<i32> = row.get("extended_time");