我正在使用rust-postgres library,我想做一个SELECT并检查第一行的第一列是否为NULL。
这就是我获取数据的方式:
let result = connection.query(
r#"
SELECT structure::TEXT
FROM sentence
WHERE id = $1
"#,
&[&uuid]
);
let rows = result.expect("problem while getting sentence");
let row = rows
.iter()
.next() // there's only 1 result
.expect("0 results, expected one...");
我发现解决它的唯一简单方法是以下代码:
match row.get_opt(0) {
Some(Ok(data)) => some data found,
Some(Err(_)) => the column is null,
None => out of bound column index
}
不幸的是,似乎Some(Err(_))
是任何类型的SQL /数据库错误的执行路径,而且不仅仅是检索到的列是NULL。
我应该使用哪种条件来检查列是否为NULL?