pub struct Row { /* private fields */ }pg only.Expand description
A database row result.
There are two representations of a SQLite row in the SDK. This type is useful for addressing elements by column name, and is obtained from the QueryResult::next() function. The DbValue vector representation is obtained from the QueryResult::rows() function, and provides index-based lookup or low-level access to row values via a vector.
Implementations§
Source§impl Row
impl Row
Sourcepub fn get<T: Decode>(&self, column: &str) -> Option<T>
pub fn get<T: Decode>(&self, column: &str) -> Option<T>
Get a value by its column name. The value is converted to the target type as per the conversion table shown in the module documentation.
This function returns None for both no such column and failed conversion. You should use
it only if you do not need to address errors (that is, if you know that conversion should
never fail). If your code does not know the type in advance, use the raw QueryResult::rows() function
instead of the QueryResult::next() or QueryResult::collect() wrappers to access
the underlying DbValue enum: this will allow you to
determine the type and process it accordingly.
Additionally, this function performs a name lookup each time it is called. If you are iterating over a large number of rows, it’s more efficient to use column indexes, either calculated or statically known from the column order in the SQL.
§Examples
use spin_sdk::pg::{Connection, DbValue};
let db = Connection::open("host=localhost user=postgres password=my_password dbname=mydb").await?;
let mut query_result = db.query(
"SELECT * FROM users WHERE id = $1",
&[user_id.into()]
).await?;
let user_row = query_result.next().await.unwrap();
let name = user_row.get::<String>("name").unwrap();
let age = user_row.get::<i16>("age").unwrap();