gpt4 book ai didi

sqlite - 特征 `rusqlite::types::to_sql::ToSql` 未针对 `serde_json::value::Value` 实现

转载 作者:行者123 更新时间:2023-12-03 11:29:54 30 4
gpt4 key购买 nike

我正在尝试从 serde_json::Map 序列化 JSON 值到 SQLite 数据库中。我想在 Map 中使用多种数据类型并将它们转换为适当的 SQLite 数据类型。
map 创建于 collect_values并传递给 write_record功能。 new_db函数创建一个 rustqlite::Connection传递给 write_record 的上下文
但是,当我尝试从 map 中插入值时,出现此错误

error[E0277]: the trait bound `serde_json::value::Value: rusqlite::types::to_sql::ToSql` is not satisfied
--> src/main.rs:51:9
|
51 | / params![
52 | | &values.get("TEST-KEY-1").unwrap_or(&missing_value),
53 | | &values.get("TEST-KEY-2").unwrap_or(&missing_value)
54 | | ],
| |_________^ the trait `rusqlite::types::to_sql::ToSql` is not implemented for `serde_json::value::Value`
|
= note: required because of the requirements on the impl of `rusqlite::types::to_sql::ToSql` for `&serde_json::value::Value`
= note: required because of the requirements on the impl of `rusqlite::types::to_sql::ToSql` for `&&serde_json::value::Value`
= note: required for the cast to the object type `dyn rusqlite::types::to_sql::ToSql`
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
我需要手动实现序列化器吗?我以为是rusqlite的 types 模块已经完成了这项工作。
Cargo.toml
[package]
name = "sqlite-min-example"
version = "0.1.0"
authors = ["test"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rusqlite = "0.23.1"
serde_json = "1.0"
serde = {version = "1.0.113", default-features = false}
main.rs
use rusqlite::{params, Connection, Result};
use serde_json::json;
use serde_json::map::Map;

fn main() {
println!("Opening connection");

let conn = new_db();

match conn {
Ok(ctx) => {
let mp = collect_values();
let res = insert_record(&mp, &ctx);
}
Err(e) => {
eprintln!("{}", e);
}
}
}

pub fn new_db() -> Result<rusqlite::Connection> {
let conn = Connection::open("test.db")?;
//let conn = Connection::open_in_memory()?;
conn.execute(
"CREATE TABLE IF NOT EXISTS testdb (
field1 INTEGER,
field2 INTEGER
)",
params![],
)?;
// return the new connection context (object)
Ok(conn)
}

pub fn insert_record(
values: &serde_json::map::Map<std::string::String, serde_json::value::Value>,
conn: &rusqlite::Connection,
) -> Result<()> {
// Insert this if we can't find a value for a key for some reason...
let missing_value = json!("MISSINGVAL");

conn.execute(
"INSERT INTO testdb
(
field1,
field2
)
VALUES (
?1, ?2
)",
params![
&values.get("TEST-KEY-1").unwrap_or(&missing_value),
&values.get("TEST-KEY-2").unwrap_or(&missing_value)
],
)?;
// return any errors that occured
Ok(())
}

pub fn collect_values() -> serde_json::map::Map<std::string::String, serde_json::value::Value> {
// Take in the Modbus context and return a map of keys (field names) and their associated values

let mut map = Map::new();

map.insert("TEST-KEY-1".to_string(), json!(1234));
map.insert("TEST-KEY-2".to_string(), json!(5678));

return map;
}

最佳答案

在 Cargo.toml 中为 rusqlite 添加适当的功能:

rusqlite = { version = "0.23.1", features = ["serde_json"] }
也可以看看:
  • How do you enable a Rust "crate feature"?
  • How to tell what "features" are available per crate?
  • 关于sqlite - 特征 `rusqlite::types::to_sql::ToSql` 未针对 `serde_json::value::Value` 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62662603/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com