首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >属性`rusqlite::types::to_sql::ToSql`不是为`serde_json::value::Value`实现的

属性`rusqlite::types::to_sql::ToSql`不是为`serde_json::value::Value`实现的
EN

Stack Overflow用户
提问于 2020-06-30 17:04:19
回答 1查看 1.2K关注 0票数 2

我正在尝试将serde_json::Map的JSON值序列化到SQLite数据库中。我希望在Map中使用多个数据类型,并将它们转换为适当的SQLite数据类型。

映射是在collect_values中创建的,并传递给write_record函数。new_db函数创建传递给write_recordrustqlite::Connection上下文

但是,当我试图从映射中插入值时,我会得到以下错误

代码语言:javascript
复制
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

代码语言:javascript
复制
[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

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-30 17:14:31

将适当的特性添加到Cargo.toml中的rusqlite中:

代码语言:javascript
复制
rusqlite = { version = "0.23.1", features = ["serde_json"] }

另请参阅:

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62662603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档