Skip to main content

Connection

Struct Connection 

Source
pub struct Connection(/* private fields */);
Available on crate feature 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

Source

pub async fn open_default() -> Result<Self, Error>

Open a connection to the default database

Source

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.

Source

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

Source

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.

Source

pub async fn changes(&self) -> u64

The number of rows modified, inserted or deleted by the most recently completed INSERT, UPDATE or DELETE statement on the connection.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V