Skip to main content

Documentation 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.

The Row trait is the primary interface for reading data out of a query result. Every database driver returns its own concrete row type (PgRow, MySqlRow, SqliteRow, AnyRow), but they all implement the same Row trait so application code can stay generic. Columns within a row are accessed either by position (usize) or by name (&str) through the ColumnIndex trait.

Row trait

The Row trait provides methods for decoding column values and inspecting row structure.

Value access

Decodes the value at index into type T. Panics if the column does not exist or if the value cannot be decoded into T.
let name: String = row.get("name");
let id: i64 = row.get(0);
Use this when you are certain the column exists and the type is correct. For fallible access, prefer try_get.
Decodes the value at index into type T, returning an Error instead of panicking on failure.
let name: String = row.try_get("name")?;
let id: i64 = row.try_get(0)?;
Unlike get, try_get does not perform a compile-time type compatibility check; it attempts the decode unconditionally and surfaces mismatches as errors at runtime.
Like get, but skips the database-type compatibility check and blindly attempts to decode the raw value. Panics on decode failure. Prefer get unless you have a specific reason to bypass the check.
Like try_get, but skips the compatibility check. Returns an error on decode failure rather than panicking.

Column introspection

MethodReturn typeDescription
columns()&[DB::Column]Returns a slice of all columns in the row.
column(index)&DB::ColumnReturns the column at index. Panics if out of bounds.
try_column(index)Result<&DB::Column, Error>Returns the column at index or a ColumnIndexOutOfBounds error.
len()usizeReturns the number of columns in the row.
is_empty()boolReturns true if the row has no columns.

Handling NULL values

SQL NULL maps to Rust’s Option<T>. Use Option<T> as the type parameter in get or try_get to handle nullable columns without an error.
// Returns None if the column value is NULL
let nickname: Option<String> = row.try_get("nickname")?;

// Panics if the value is NULL and you used a non-Option type
let nickname: String = row.get("nickname"); // panics on NULL
Decoding a NULL column value into a non-Option type returns an error from try_get and panics from get. Always use Option<T> for nullable columns.

ColumnIndex trait

The ColumnIndex trait is implemented by both usize (positional access) and &str (name-based access), giving you two ways to index into a row.
// By position
let first: i64 = row.get(0);

// By name
let id: i64 = row.get("id");
Name-based indexing performs a linear scan over the column list. For performance-critical hot paths with many columns, positional access by usize is faster.

Column trait

Every database driver’s column type implements the Column trait, which exposes metadata about a single column returned by a query.
MethodReturn typeDescription
name()&strThe column name or alias. Unreliable for unaliased expressions.
ordinal()usizeZero-based position of the column in the row.
type_info()&DB::TypeInfoDatabase-level type information for the column.
origin()ColumnOriginSource table and original column name, if known.
ColumnOrigin has three variants:
  • ColumnOrigin::Table(TableColumn) — the column comes from a known table; includes table and name fields.
  • ColumnOrigin::Expression — the column is computed from an expression or origin could not be determined.
  • ColumnOrigin::Unknown — the driver does not provide origin information (the default).
for col in row.columns() {
    println!("col {} (ordinal {}): {:?}", col.name(), col.ordinal(), col.type_info());
}

Database row types

Each database driver provides a concrete row type that implements Row.

PgRow

Returned by PostgreSQL queries. Requires the postgres feature.

MySqlRow

Returned by MySQL/MariaDB queries. Requires the mysql feature.

SqliteRow

Returned by SQLite queries. Requires the sqlite or sqlite-unbundled feature.

AnyRow

A type-erased row that works with any driver at runtime. Requires the any feature. Not all TypeInfo detail is available.

Build docs developers (and LLMs) love