mirror of
https://github.com/tursodatabase/limbo.git
synced 2025-12-23 08:21:09 +00:00
Improved transaction example
It is more realistic to begin and commit the transaction outside the update function.
This commit is contained in:
parent
9f7fc532b9
commit
357a24580b
1 changed files with 3 additions and 3 deletions
|
|
@ -15,13 +15,12 @@ async fn create_tables(conn: &Connection) -> Result<(), Error> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn insert_users(tx: Transaction<'_>) -> Result<(), Error> {
|
||||
async fn insert_users(tx: &Transaction<'_>) -> Result<(), Error> {
|
||||
let mut stmt = tx
|
||||
.prepare("INSERT INTO users (email, age) VALUES (?1, ?2)")
|
||||
.await?;
|
||||
stmt.execute(["foo@example.com", &21.to_string()]).await?;
|
||||
stmt.execute(["bar@example.com", &22.to_string()]).await?;
|
||||
tx.commit().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -53,7 +52,8 @@ async fn main() -> Result<(), Error> {
|
|||
|
||||
create_tables(&conn).await?;
|
||||
let tx = conn.transaction().await?;
|
||||
insert_users(tx).await?;
|
||||
insert_users(&tx).await?;
|
||||
tx.commit().await?;
|
||||
list_users(&conn).await?;
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue