How to handle transactions in a SQLite database?
How to handle transactions in a SQLite database?
How to handle transactions in a SQLite database?
solveurit24@gmail.com Changed status to publish February 13, 2025
Transactions are managed with commit() and rollback().
import sqlite3
conn = sqlite3.connect('mydatabase.db')
cursor = conn.cursor()
try:
cursor.execute("BEGIN TRANSACTION")
cursor.execute("INSERT INTO users (name) VALUES ('Charlie')")
cursor.execute("COMMIT")
except:
cursor.execute("ROLLBACK")
print("Transaction failed.")
conn.close()solveurit24@gmail.com Changed status to publish February 13, 2025