pub struct Connection(/* private fields */);sqlite only.Expand description
An open connection to a SQLite database.
Connection::execute() returns a tuple of (columns, rows_stream, finish_future)
where rows are consumed from a stream and the finish future is awaited to check
for errors.
§Examples
Open the default database, query rows, and iterate over the stream.
use spin_sdk::sqlite::{Connection, Value};
let min_age = 0;
let db = Connection::open_default().await?;
let mut query_result = db.execute(
"SELECT * FROM users WHERE age >= ?",
[Value::Integer(min_age)],
).await?;
let name_idx = query_result.columns().iter().position(|c| c == "name").unwrap();
while let Some(row) = query_result.next().await {
let name: &str = row.get(name_idx).unwrap();
println!("Found user {name}");
}
query_result.result().await?;Perform an aggregate (scalar) operation over a named database.
use spin_sdk::sqlite::Connection;
let db = Connection::open("customer-data").await?;
let mut query_result = db.execute("SELECT COUNT(*) FROM users", []).await?;
if let Some(row) = query_result.next().await {
let count: i64 = row.get(0).unwrap();
println!("Total users: {count}");
}
query_result.result().await?;Delete rows from a database. The row stream will be empty, but the finish future must still be awaited.
use spin_sdk::sqlite::{Connection, Value};
let min_age = 18;
let db = Connection::open("customer-data").await?;
let query_result = db.execute(
"DELETE FROM users WHERE age < ?",
[Value::Integer(min_age)],
).await?;
query_result.result().await?;Implementations§
Source§impl Connection
impl Connection
Sourcepub async fn open_default() -> Result<Self, Error>
pub async fn open_default() -> Result<Self, Error>
Open a connection to the default database
Sourcepub async fn open(database: impl AsRef<str>) -> Result<Self, Error>
pub async fn open(database: impl AsRef<str>) -> Result<Self, Error>
Open a connection to a named database instance.
If database is “default”, the default instance is opened.
error::no-such-database will be raised if the name is not recognized.
Sourcepub async fn execute(
&self,
statement: impl AsRef<str>,
parameters: impl IntoIterator<Item = Value>,
) -> Result<QueryResult, Error>
pub async fn execute( &self, statement: impl AsRef<str>, parameters: impl IntoIterator<Item = Value>, ) -> Result<QueryResult, Error>
Execute a statement returning back data if there is any
Sourcepub async fn last_insert_rowid(&self) -> i64
pub async fn last_insert_rowid(&self) -> i64
The SQLite rowid of the most recent successful INSERT on the connection, or 0 if there has not yet been an INSERT on the connection.