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
1
vote
0 answers
Is there a way to get sqlx to print the SQL code that it generates (after binding parameters)?
I have a String called main_sql and a Vec called main_params.
main_sql looks something like: INSERT INTO "foo" ("col1", "col2", "col3", "col4") VALUES (?, ?, NULL, ?)
main_params looks like ["some string", "another string", "yet another…

The bassist
- 23
- 4
1
vote
0 answers
Can sqlx infer a non-NULL output column for a JOIN query?
For two postgresql tables, a and b with non-null columns
struct MyStruct {
id: i32,
name: String,
}
sqlx::query_as!(MyStruct, r"
SELECT a.id, b.name
FROM a
INNER JOIN b on a.id = b.a
")
results in errors like the following
mismatched…

Cypress Frankenfeld
- 2,317
- 2
- 28
- 40
1
vote
1 answer
How can I get an Axum Handler function to return a Vec?
I'm learning how to use Axum with SQLx starting with this example. The basic example works, but I have problem trying to move forward. I am working with a simple database table as shown below:
todo | description
--------+--------------
todo_1 |…

kinxiel
- 793
- 1
- 5
- 7
1
vote
1 answer
optional feature `uuid` required for type UUID of column #1 ("id")
I'm trying to query a PostgreSQL database table using sqlx. But, the following error is shown
[rustc] [E] optional feature `uuid` required for type UUID of column #1 ("id")
My code snippet
use sqlx::{Pool, Postgres, postgres::PgRow, Row,…

Roman Mahotskyi
- 4,576
- 5
- 35
- 68
1
vote
1 answer
Retrieve Postgres TSTZRANGE to PgRange> with Rust sqlx
I have the following SQL table scheme
CREATE TABLE reservation (
id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
timespan TSTZRANGE
);
INSERT INTO reservation(timespan) VALUES
(TSTZRANGE(now() + INTERVAL '0 hour', now() +…

Blank
- 423
- 1
- 5
- 16
1
vote
0 answers
Mapping results from mysql procedures in Rust (using sqlx)
Need:
I have a solid query with named parameters to run in my backend. The query contains many reference to those parameters and, as far as i'm concerned, it's not currently possible to use named parameters query with sqlx for mysql features. So i…

Esotopo21
- 392
- 1
- 12
1
vote
1 answer
Why does mutably borrowing fix an expected trait implementation?
I am using rust sqlx and I am getting a connection from my DB pool for my query:
POOL: OnceCell> = OnceCell::new();
pub async fn get_connection() -> PoolConnection {
POOL.get().unwrap().acquire().await.unwrap()
}
pub…

rustgamer3434
- 43
- 1
- 6
1
vote
0 answers
Generic query function
Im not sure if what im asking for here is even possible in Rust but ill give it an ask anyway. So thanks to some helpful people on here I am able to use the sqlx::query_as! macro to query data from a mysql database and return it as the passed in…

discodowney
- 1,475
- 6
- 28
- 58
1
vote
1 answer
Select query returns no data
I have the following code:
use sqlx::mysql::*;
mod db;
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
println!("Hello, world!");
let pool = MySqlPoolOptions::new()
.max_connections(5)
…

discodowney
- 1,475
- 6
- 28
- 58
1
vote
2 answers
How to test two parallel transactions in Rust SQLx?
I'm experimenting with Rocket, Rust and SQLx and I'd like to test what happens when two parallel transactions try to insert a duplicated record on my table.
My insert fn contains nothing special and it works fine:
async fn insert_credentials<'ex,…

André
- 12,497
- 6
- 42
- 44
1
vote
1 answer
Rust warp+sqlx service : idiomatic way of passing DBPool from main to handlers
A Rust newbie here, attempting to write a webservice by combining
https://github.com/seanmonstar/warp/blob/master/examples/todos.rs and https://github.com/launchbadge/sqlx/blob/master/examples/postgres/todos/src/main.rs
The following code is in…

so-random-dude
- 15,277
- 10
- 68
- 113
1
vote
1 answer
Storing disparate SQLX types in a vector
As the title suggests, I want to store various SQLX types in a vector for manual query building. Here is my best stab at it:
use sqlx::{Database, Encode, Type};
use sqlx::database::HasArguments;
use sqlx::query::QueryScalar;
pub type SqlVec<'q, DB:…

eltiare
- 1,817
- 1
- 20
- 28
1
vote
1 answer
Unable to pass DATABASE_URL and BIN NAME flags to cargo prepare for sqlx in offline mode
I'm trying to run postgres database in offline mode using the sqlx cli by running the command sqlx prepare to prepare the sqlx-data.json file to work offline per these…

AMP_035
- 167
- 1
- 2
- 13
1
vote
0 answers
How to get State in FromRequest implementation with Rocket?
I initialized a Mutex> instance and passed it to manage method on Rocket instance in order to access the database on my controllers. What I usually do is:
// this is purely for example
#[get("/foo/bar")]
pub async fn controller<'a>(
…

Eray Erdin
- 2,633
- 1
- 32
- 66
1
vote
1 answer
Create adapter for sqlx Value
I've generic trait in the app to convert results across different storage. Now I want to add support for SQLx. I want to keep it generic for all sqlx engines.
Simplified example for i64:
// My trait
pub trait TryConvert {
fn try_i64(&self) ->…

Alex
- 49
- 2
- 6