无法调用 rusqlite 的查询,因为它需要类型 &[&rusqlite::types::ToSql] [英] Cannot call rusqlite's query because it expects the type &[&rusqlite::types::ToSql]

查看:53
本文介绍了无法调用 rusqlite 的查询,因为它需要类型 &[&rusqlite::types::ToSql]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有 rusqlite 的准备好的语句.Rusqlite 为 String&str一堆其他类型:

I want to use a prepared statement with rusqlite. Rusqlite implements the trait ToSql for String, &str and a bunch of other types:

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 对象 是一种不同的类型,并且通常与底层类型的大小不同;在将值打包到向量中时,两者都是重要的考虑因素.

The compiler message isn't lying to you. You have a &[&String] not a &[&ToSql]. A trait object is a different type and often a different size from the underlying type; both are important considerations when packing values into a vector.

另一个问题是你不能创建一个String,引用它,然后把它存储在一个变量中.String 将立即被释放,留下一个悬空引用,因此编译器会阻止这种情况发生.

Another problem is that you cannot create a String, take a reference to it, then store that in a variable. The String would be deallocated immediately, leaving a dangling reference, so the compiler prevents that.

您可以做的最简单的事情是创建一个包含特征对象引用的新 Vec:

The easiest thing you can do is to create a new Vec that contains the trait object references:

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

(完整示例)

或者,如果您想要一个过于通用的函数来执行转换:

Or if you wanted an overly-generic function to perform the conversion:

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

(完整示例)

在许多情况下,您可以使用参数!named_pa​​rams! 宏:

In many cases, you can use the params! or named_params! macro:

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

(完整示例)

这篇关于无法调用 rusqlite 的查询,因为它需要类型 &amp;[&amp;rusqlite::types::ToSql]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆