-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path37. Database Transaction.py
33 lines (24 loc) · 1.37 KB
/
37. Database Transaction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Here is an example of how to use a database transaction in Python using the built-in sqlite3 module:
import sqlite3
# Connect to database
conn = sqlite3.connect('mydatabase.db')
# Create a cursor object
cursor = conn.cursor()
# Begin transaction
cursor.execute("BEGIN")
try:
# Execute database operations
cursor.execute("INSERT INTO customers (name, email) VALUES (?, ?)", ('John Doe', 'johndoe@example.com'))
cursor.execute("UPDATE customers SET email = ? WHERE name = ?", ('janedoe@example.com', 'Jane Doe'))
# Commit transaction
conn.commit()
print("Transaction committed")
except Exception as e:
# Rollback transaction if an error occurs
conn.rollback()
print("Transaction rolled back")
finally:
# Close database connection
conn.close()
# In this example, we begin a transaction using the BEGIN statement, then execute some database operations (an INSERT and an UPDATE statement) within the transaction. If no errors occur, we commit the transaction using the commit() method. If an error occurs, we rollback the transaction using the rollback() method to undo any changes made during the transaction. Finally, we close the database connection using the close() method.
# Note that the specific database operations (i.e. the INSERT and UPDATE statements in this example) will depend on your specific use case and the structure of your database.