SQLx provides a small family of free functions and a builder type that cover the full range of query execution patterns: from simple one-shot statements that return raw rows, to typed struct mapping, to dynamically constructed bulk inserts. Every function in this family produces a type that acceptsDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/launchbadge/sqlx/llms.txt
Use this file to discover all available pages before exploring further.
.bind() calls for parameterized input and terminates with an executor method such as .fetch_one() or .execute().
All query functions require a runtime feature (
runtime-tokio, runtime-async-std, etc.) to be enabled. The query!() macro family is documented separately on the Query macros reference page.sqlx::query
SELECT, INSERT, UPDATE, DELETE, and variants). Use sqlx::raw_sql for DDL or multi-statement batches.
Bind parameters are database-specific:
- Postgres / SQLite:
$1,$2,$3, … (the same placeholder may be reused) - MySQL / MariaDB:
?(purely positional; repeat the bind call to use a value multiple times)
Query<'_, DB, DB::Arguments>
.bind(value)
Appends a bind parameter to the query. The call order must match the placeholder order in the SQL string (for MySQL/MariaDB) or the placeholder number (for Postgres/SQLite).
.persistent(bool)
Controls whether the prepared statement is cached on the connection (default: true). Set to false when the query string varies across calls, for example inside QueryBuilder, to avoid filling the statement cache.
sqlx::query_with
query() but accepts a pre-built Arguments object instead of building one incrementally with .bind(). Useful when constructing arguments programmatically.
Returns: Query<'_, DB, A>
sqlx::query_as
query(), but each returned row is automatically decoded into type O via the [FromRow] trait. O may be a named struct with #[derive(sqlx::FromRow)], or a tuple of up to 16 elements (columns matched by position).
Returns: QueryAs<'_, DB, O, DB::Arguments>
- Named struct
- Tuple
sqlx::query_as_with
query_as with pre-built arguments, analogous to query_with.
sqlx::query_scalar
O. Additional columns are silently ignored. This is the most convenient API for aggregate queries (COUNT, MAX, SUM, EXISTS) and single-column lookups.
Returns: QueryScalar<'_, DB, O, DB::Arguments>
sqlx::QueryBuilder
QueryBuilder constructs queries at runtime by concatenating SQL fragments and bind parameters. Use it when the shape of the query — the number of columns, filters, or VALUES rows — is not known at compile time.
QueryBuilder::new(init)
"INSERT INTO users(id, name) " or "SELECT * FROM t WHERE ".
.push(sql)
.push_bind(value)
$N or ?) and records the value. This is the safe path for dynamic input.
.push_values(tuples, builder_fn)
VALUES (…), (…) clause from an iterator of tuples. The callback receives a Separated helper and the current item; call .push_bind() on the helper for each column value.
.separated(separator)
Separated helper that inserts separator before each subsequent push (but not before the first). Useful for IN (…) clauses and comma-separated lists.
.build()
Query ready for execution. The builder must be reset (via .reset()) before reuse.
sqlx::raw_sql
CREATE TABLE, ALTER TABLE), multi-statement batches, and any SQL the database does not support as a prepared statement.
Unlike query(), raw_sql does not prepare or cache the statement, and bind parameters are not supported. Use it for trusted, static SQL only.
Returns: RawSql<'_>
Query finalizers
All query types (Query, QueryAs, QueryScalar, Map, RawSql) expose the same set of terminal async methods. Call one of these to actually send the SQL to the database.
.execute(executor)
.execute(executor)
Runs the query and returns the database’s
QueryResult (rows affected / last insert ID). Discards any rows returned..fetch_one(executor)
.fetch_one(executor)
Executes and returns exactly the first row, or For best performance, write queries that naturally return at most one row (filtering by a unique key), or add
Error::RowNotFound if the result set is empty.LIMIT 1..fetch_optional(executor)
.fetch_optional(executor)
Executes and returns
Some(row) for the first row, or None if the result set is empty..fetch_all(executor)
.fetch_all(executor)
Executes and collects all rows into a
Vec. Use with care — if the result set is unbounded this will exhaust available memory. Always pair with a LIMIT clause when possible..fetch(executor)
.fetch(executor)
Returns a
BoxStream that yields rows lazily. Use when you want to process rows one at a time without loading the entire result set into memory.