pub struct RowResult {
pub values: Vec<Value>,
}
Expand description
A database row result.
There are two representations of a SQLite row in the SDK. This type is obtained from the QueryResult::rows field, and provides index-based lookup or low-level access to row values via a vector. The Row type is useful for addressing elements by column name, and is obtained from the QueryResult::rows() function.
§Examples
Load a set of rows from the default SQLite database, and iterate over them selecting one field from each. The example caches the index of the desired field to avoid repeated lookup, making this more efficient than the Row-based equivalent at the expense of extra code and inferior readability.
use spin_sdk::sqlite::{Connection, Value};
let db = Connection::open_default()?;
let query_result = db.execute(
"SELECT * FROM users WHERE age >= ?",
&[Value::Integer(min_age)]
)?;
let name_index = query_result.columns.iter().position(|c| c == "name").unwrap();
for row in &query_result.rows {
let name: &str = row.get(name_index).unwrap();
println!("Found user {name}");
}
A set of values for each of the columns in a query-result
Fields§
§values: Vec<Value>
Implementations§
Source§impl RowResult
impl RowResult
Sourcepub fn get<'a, T: TryFrom<&'a Value>>(&'a self, index: usize) -> Option<T>
pub fn get<'a, T: TryFrom<&'a Value>>(&'a self, index: usize) -> Option<T>
Get a value by its column name. The value is converted to the target type.
- SQLite integers are convertible to Rust integer types (i8, u8, i16, etc. including usize and isize) and bool.
- SQLite strings are convertible to Rust &str or &u8 (encoded as UTF-8).
- SQLite reals are convertible to Rust f64.
- SQLite blobs are convertible to Rust &u8 or &str (interpreted as UTF-8).
To look up by name, you can use QueryResult::rows()
or obtain the invoice from QueryResult::columns
.
If you do not know the type of a value, access the underlying Value enum directly
via the RowResult::values field
§Examples
use spin_sdk::sqlite::{Connection, Value};
let db = Connection::open_default()?;
let query_result = db.execute(
"SELECT name, age FROM users WHERE id = ?",
&[Value::Integer(user_id)]
)?;
let user_row = query_result.rows.first().unwrap();
let name = user_row.get::<&str>(0).unwrap();
let age = user_row.get::<u16>(1).unwrap();