The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, SQLite, and MSSQL. Don't confuse with the sqlx tag, which is a GO library.
Questions tagged [rust-sqlx]
145 questions
11
votes
1 answer
How to build and commit multi-query transaction in sqlx?
In sqlx there's a Transaction type which lets you run multiple queries in one transaction.
I'm trying to figure out how to do this, which sadly is not documented, although there is the automatically generated API docs.
My first attempt:
async fn…

Martin Josefsson
- 953
- 12
- 24
10
votes
3 answers
Generic function that accepts a SQLx PgPool or MySqlPool
I'd like to setup a generic function that accepts a SQLx PgPool or MySqlPool.
use dotenv::dotenv;
use sqlx::postgres::PgPool;
use sqlx::{Pool, Database};
use std::env;
#[derive(Debug)]
struct Todo {
id: i64,
description: String,
done:…

patrick-fitzgerald
- 2,561
- 3
- 35
- 49
9
votes
2 answers
How to create a SQLite database with rust sqlx
I'm trying out a conversion from rusqlite => sqlx.
Opening a connection from rusqlite calls SQLite::open, and creates the db files. The following works:
use rusqlite::Connection;
Connection::open(db_filename)
However, I'm following the docs on the…

cdaringe
- 1,274
- 2
- 15
- 33
9
votes
0 answers
rust sqlx get error "pool timed out while waiting for an open connection"
Sqlx reported an error when I used the script to loop the request
"pool timed out while waiting for an open connection"
this pool is below
Pool { size: 5, num_idle: 5, is_closed: false, options: PoolOptions { max_connections: 5, min_connections:…

周永建
- 113
- 6
8
votes
2 answers
How does one use `sqlx` with `juniper` subscriptions in Rust?
Background:
I am having trouble integrating sqlx with juniper subscriptions.
I am getting a Pin> + 'e + Send>> from sqlx::query::QueryAs::fetch().
juniper needs subscriptions to be returned as…

ehegnes
- 81
- 1
8
votes
0 answers
How to re-use an sqlx::Executor?
I'm trying to develop an app server in Rust using SQLX for back-end storage. It's for a primitive CMS with some idiosyncratic default behavior, to wit: if the page is not found, generate it automatically with some blank content. I want to be able…

Elf Sternberg
- 16,129
- 6
- 60
- 68
7
votes
1 answer
Is there a way to use dynamic query in `sqlx::query!()"
Context: rust, library sqlx
Question: how to compose similar queries from smaller parts, without losing type check?
macro_rules! select {
() => {"select col from tab"}
}
macro_rules! guard1 {
() => {"where col > 1"}
}
macro_rules! guard2 {
…

Incömplete
- 853
- 8
- 20
6
votes
2 answers
How to query using an IN clause and a `Vec` as parameter in Rust sqlx for MySQL?
Note: this is a similar but NOT duplicate question with How to use sqlx to query mysql IN a slice?. I'm asking for the Rust one.
This is what I try to do.
let v = vec![..];
sqlx::query("SELECT something FROM table WHERE column IN (?)").bind(v)
…

Jason Lee
- 502
- 6
- 15
6
votes
1 answer
How do I implement sqlx::FromRow trait maually?
I am trying to manually implement sqlx::FrowRow instead of using derive, since some custom initialization logic is needed (ex. convert integer i16 to enum).
Default impl is generated like this:
use sqlx::FromRow;
impl FromRow for User {
fn…

Fellow Rustacean
- 75
- 1
- 5
6
votes
1 answer
How to define a Vec of Enums as a field in rust sqlx model
I am trying to load an array field in Postgres to a Rust struct as follows
use sqlx::{Pool, PgConnection, PgPool, FromRow};
use sqlx::postgres::PgQueryAs;
#[derive(Copy, Clone, sqlx::Type)]
#[sqlx(rename = "VARCHAR")]
#[sqlx(rename_all =…

Asnim P Ansari
- 1,932
- 1
- 18
- 41
6
votes
1 answer
Rust sqlx: there is no query finalizers
The issue is when I type code like this.
let data = sqlx::query("SELECT id, name FROM test")
.fetch_all(&pool)
.await;
I get an no method named `fetch_all` found for struct `sqlx_core::query::Query<'_, _>` in the current scope…

Link0
- 655
- 5
- 17
5
votes
1 answer
How to build safe dynamic query with sqlx in rust?
sqlx has a query builder. Documentation can be seen here
I see it supports dynamically buidling queries of the form:
SELECT * FROM users WHERE (id, username) IN ((1, "test_user_1"), (2, "test_user_2"))
But I am interested in building more complex…

Finlay Weber
- 2,989
- 3
- 17
- 37
5
votes
1 answer
Sqlx only accept string literal in query macro, how to use variable?
The codes:
// very complex where clause combined from several runtime variables.
let query: String = String.from("where ..........");
let rows_num: i64 = sqlx::query!( &*query).fetch_one(connection)
The error from compiler:
error: expected string…

Ethan
- 165
- 1
- 9
5
votes
0 answers
How to mock transactions in rust with sqlx?
I'm currently building an application with actix-web and sqlx. What I've build as an architecture is very similar to this source.
This is basically a trait wrapping the db access, so far so good. But this assumes every single method will get a…

Regyn
- 585
- 3
- 17
5
votes
0 answers
Rust `sqlx` complains about missing trait implementations
I have the following insert query:
pub async fn create_property(
&self,
property: Property,
) -> Result {
/* acquire connection from the pool */
let mut conn =…

jmcph4
- 197
- 2
- 8