我想对 rusqlite 使用准备好的语句. Rusqlite 为 String
、&str
和 a bunch of other types 实现了特征 ToSql
:
extern crate rusqlite;
use rusqlite::Connection;
fn main() {
let mut connection = Connection::open("C:\\test_db.db").unwrap();
let mut cached_statement = connection
.prepare_cached("SELECT ?, ?, ? FROM test")
.unwrap();
let vec_values = vec![
&"test1".to_string(),
&"test2".to_string(),
&"test3".to_string(),
];
let rows = cached_statement.query(vec_values.as_slice()).unwrap();
}
这不会编译错误:
error[E0308]: mismatched types
--> src/main.rs:18:39
|
18 | let rows = cached_statement.query(vec_values.as_slice()).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^ expected trait rusqlite::types::ToSql, found struct `std::string::String`
|
= note: expected type `&[&rusqlite::types::ToSql]`
found type `&[&std::string::String]`
最佳答案
编译器信息没有骗你。您有一个 &[&String]
而不是 &[&ToSql]
。 trait 对象 是一种不同的类型,并且通常与底层类型的大小不同;将值打包到向量中时,两者都是重要的考虑因素。
另一个问题是您不能创建一个 String
,引用它,然后将其存储在一个变量中。 String
会立即被释放,留下悬空引用,因此编译器会阻止这种情况发生。
您可以做的最简单的事情是创建一个包含特征对象引用的新 Vec
:
let vec_values = vec![
"test1".to_string(),
"test2".to_string(),
"test3".to_string(),
];
let query_values: Vec<_> = vec_values.iter().map(|x| x as &dyn ToSql).collect();
let _rows = cached_statement.query(&query_values).unwrap();
( complete example )
或者如果您想要一个过于通用的函数来执行转换:
fn do_the_thing<'a, I, T: 'a>(things: I) -> Vec<&'a dyn ToSql>
where
I: IntoIterator<Item = &'a T>,
T: ToSql,
{
things.into_iter().map(|x| x as &dyn ToSql).collect()
}
let _rows = cached_statement.query(&do_the_thing(&vec_values)).unwrap();
( complete example )
在很多情况下,您可以使用 params!
或 named_params!
宏观:
let a = "test1".to_string();
let b = "test2".to_string();
let c = "test3".to_string();
let _rows = cached_statement.query(params![a, b, c]).unwrap();
( complete example )
关于rust - 无法调用 rusqlite 的查询,因为它需要类型 &[&rusqlite::types::ToSql],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46624591/