Created
January 29, 2022 12:31
-
-
Save Shine-neko/12b5e71e0a6754f6ecd37639a76a8d3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "app" | |
version = "0.1.0" | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
env_logger = "0.6.0" | |
serde = { version = "1.0", features = ["derive"] } | |
serde_json = "1" | |
serde_rusqlite = "0.29.0" | |
rusqlite = "0.26.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@143389edf290:/app# rm ruru.db && cargo run | |
Compiling app v0.1.0 (/app) | |
Finished dev [unoptimized + debuginfo] target(s) in 0.47s | |
Running `target/debug/app` | |
Err(Deserialization("invalid type: string \"{\\\"key\\\": \\\"value\\\"}\", expected a map")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use rusqlite::{params, Connection, Result}; | |
use serde_rusqlite::from_rows_ref; | |
use serde::Serialize; | |
use serde::Deserialize; | |
use rusqlite::named_params; | |
use std::collections::HashMap; | |
#[derive(Serialize, Deserialize, Debug, Clone)] | |
struct User { | |
id: i32, | |
username: String, | |
labels: HashMap<String, String> | |
} | |
fn main() { | |
let connection = Connection::open("ruru.db").unwrap(); | |
connection.execute("CREATE TABLE user ( | |
id INTEGER PRIMARY KEY, | |
username TEXT NOT NULL, | |
labels JSON NOT NULL | |
)", params![]).unwrap(); | |
connection.execute("INSERT INTO user (username, labels) VALUES (:username, :labels)", | |
named_params!{ | |
":username": "John Doe", | |
":labels": "{\"key\": \"value\"}" | |
} | |
).unwrap(); | |
let mut statement = connection.prepare("SELECT * FROM user LIMIT 1").unwrap(); | |
let mut rows = statement.query([]).unwrap(); | |
let mut ref_rows = from_rows_ref::<User>(&mut rows); | |
let result = ref_rows.next(); | |
let user = result.unwrap(); | |
println!("{:?}", user); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment