rusqlite | Ergonomic bindings to SQLite for Rust | Database library
kandi X-RAY | rusqlite Summary
kandi X-RAY | rusqlite Summary
Rusqlite is an ergonomic wrapper for using SQLite from Rust. It attempts to expose an interface similar to rust-postgres.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of rusqlite
rusqlite Key Features
rusqlite Examples and Code Snippets
Community Discussions
Trending Discussions on rusqlite
QUESTION
I need to convert strings that have the value null
to actual null values in a SQLite DB using rusqlite. I'm not in control of these string values. I've found the Null struct, but I'm not sure how to concisely pass it as a parameter, but then use the string value when it's not null
.
ANSWER
Answered 2022-Jan-23 at 04:06I'm not familiar with rusqlite
, but in general, the tool for “might be null” in Rust is Option
, and taking a look at rusqlite::ToSql
confirms that there is a
QUESTION
I am "serializing" and "deserializing" a generic struct to and from an SQLite database. The struct has two members whose values are of generic types, V
and T
, both constrained to the DataType
trait. When I want to reconstruct these from the information in the database, I haven't been able to find a way around specifying match arms for every combination of V
and T
. Given that I will eventually have around 20 data types, that means 20 * 20 = 400 match arms. Is there any way around this? An unsafe solution is also acceptable.
Here is a MWE with two data types:
...ANSWER
Answered 2021-Dec-17 at 10:51You can do that with the following (pretty complex) macro.
QUESTION
I have the following MWE:
...ANSWER
Answered 2021-Oct-02 at 10:56After
tx.prepare
borrows, what is ownership theTransaction
created and assigned totx
?
Not sure I understand what you're asking. A borrow doesn't change ownership, it borrows from the owner. So tx
owns the transaction object, stmt
borrows it, and thus the ownership can not change.
Is tx still the owner of the Transaction ?
Yes.
When
tx.commit()
is called, Transaction shoud be owned by tx and be safe to be moved when calling.commit()
, no ?
In most cases yes, the problem here is that Statement
is not a "trivial" type, it implements Drop
which means it will (by default) live until the end of its drop scope unless it is explicitly dropped (e.g. by calling drop(stmt)
). This, then, compiles with Transaction::commit
as you can't move out of an "active borrow".
You can observe this behaviour simply by mounting a type-scheme which matches that of Rusqlite: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=32532b41467706d0d80756525a0f43fe
As is the code compiles, but if you uncomment line 24 (impl Drop for …
) it stops compiling, with the same error you get here.
Your solutions here are:
- if the MRE matches the actual code, don't bother preparing the statement, that's only useful if you have multiple queries
- explicitly
drop()
the statement once you're done with it - or have it live in its own block, so its drop scope is shorter than the function
QUESTION
I'm learning the rust language. So, I try to build a simple web app using sqlite3. But it gets multiple packages link error.
I saw some solution for this error(ex. this), but they didn't work.
The cause seems to be that the version specification of rusqlite
is wrong, but I don't know the correct version specification.
How should I configure the cargo.toml
?
Source codes are here.
cargo.toml
ANSWER
Answered 2021-Oct-01 at 10:46You're directly depending on rusqlite
and using r2d2-sqlite3
which itself depends on rusqlite
.
Since rusqlite
binds to a native library as the message indicates you can't have two versions of rusqlite linking to different versions of sqlite3(-sys), so you need to ensure you use the same version of rusqlite as r2d2.
If you're not going to publish on Cargo, the easiest by far is to leave rusqlite
's version as a wildcard ("*"
), that way the dependency resolver will give you whatever works for r2d2-sqlite3
. Otherwise you need to check the version of r2d2-sqlite3
you're using and match it.
Incidentally... r2d2-sqlite3 0.1.1? That seems to be over 4 years old, the current version seems to be 0.18. I'm slightly surprised r2d2 works, though I guess it changes relatively little (0.8.0 was 4 years ago, current is 0.8.9). Though I'm not sure what the utility of r2d2 is for sqlite3, especially for "a simple web app".
QUESTION
use walkdir::WalkDir;
use std::ffi::OsStr;
use rusqlite::{params, Connection, Result};
use std::error::Error;
use std::io;
use std::process;
use std::path::Path;
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Trade {
id: usize,
price: f64,
quantity: f64,
quoted_quantity: f64,
time: i64,
is_buyer_maker: bool,
is_best_match: bool,
}
fn process_csv(file: &Path, db: &Connection) -> Result<(), Box> {
let mut rdr = csv::Reader::from_path(file)?;
for result in rdr.records() {
match result.deserialize::() {
Ok(s) => {
db.execute("INSERT INTO trades (id, price, quantity, quoted_quantity, time, is_buyer_maker, is_best_match) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
params![s.id, s.price, s.quantity, s.quoted_quantity, s.time, s.is_buyer_maker, s.is_best_match])?;
},
Err(e) => println!("Failed to deserialize: {}", e),
}
}
}
fn main () -> Result<(), Box> {
let conn = Connection::open("bot.db")?;
conn.execute(
"CREATE TABLE trades (
id INTEGER PRIMARY KEY,
price REAL NOT NULL,
quanity REAL NOT NULL,
quoted_quatity REAL NOT NULL,
time INTEGER NOT NULL,
is_buyer_maker INTEGER NOT NULL,
is_best_match INTEGER NOT NULL,
)",
[],
)?;
for entry in WalkDir::new("E:/binance-public-data/python/data/spot/monthly/trades/").into_iter().filter_map(|e| e.ok()) {
println!("Processing: {}", entry.path().display());
if let Err(e) = process_csv(entry.path(), &conn) {
println!("Processing failed: {}", e);
}
}
Ok(())
}
...ANSWER
Answered 2021-Sep-07 at 16:58Your first error is because you're calling csv
's .deserialize()
on the wrong variable, it should be called on the reader. Change:
QUESTION
To avoid nesting matches I am trying to use the and_then
method for Result
. The problem arises when I try to convert this
ANSWER
Answered 2021-Aug-01 at 16:51The problem is that conn.prepare
returns a statement that refers to conn
, but conn
is destroyed at the end of the closure so it's no longer here when the statement tries to use it. The idiomatic way to do what you want would be to use the ?
operator:
QUESTION
I am working on a larger private project and introduced something like a function registry for hooks to be used. That worked well so far, till I was forced to use a a type with a named lifetime In my case it was rusqlite::Transaction<'c>
. Due to this all dependend strucs has to introduce a named lifetime too, right?
Finally I got some compiler lifetime errors, I dont know, how to resolve them.
To narrow down the problem I wrote this example code. Please note that you may not change the Transaction-Struct hence this is just a example for any struc require a named lifetime parameter.
...ANSWER
Answered 2021-Jun-03 at 10:47One easy option if that's feasible is to have Holder
but f: fn(&T)
(and exe: fn(&self, &T)
:
QUESTION
I am using the rustqlite library for a SQLite database (playground)
...ANSWER
Answered 2021-May-13 at 04:56Disclaimer: I haven't tried this code. But it is only here to give you an idea of the direction to go in.
Firstly, local variables (on the stack) in Rust must be fixed sized. This is one of the problems you are facing. Transaction and Connection aren't the same sized. So you can't achieve "Polymorphism" on the stack, without some tricks.
The two ways to do this are Enum types, and Boxing (putting the structs on the Heap, and adding a VTable).
I won't go over Boxing, since that is relatively simple.
The second problem you have is that Transaction
's lifetime is tied to the Connection
, so any moving of the Transaction
will require you to move
the Connection
as well.
QUESTION
I need to fetch a row from a database using SELECT and WHERE, I need to fetch a row depending on the age, I tried this way using some other tutorial.
...ANSWER
Answered 2021-Apr-14 at 11:46You can use params
of query_map()
for this like that:
QUESTION
I have a struct with NaiveDate field and I want to insert this field to a table. This is my whole code
...ANSWER
Answered 2021-Apr-13 at 06:37You must compile with the additional chrono feature to have the FromSql
and ToSql
traits implemented for chrono types.
Change the declaration in your Cargo.toml to be
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install rusqlite
Rust is installed and managed by the rustup tool. Rust has a 6-week rapid release process and supports a great number of platforms, so there are many builds of Rust available at any time. Please refer rust-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page