-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_managers.py
53 lines (46 loc) · 1.69 KB
/
context_managers.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import sqlite3
class DatabaseConnection:
def __init__(self, db_file):
self.db_file = db_file
self.connection = None
self.cursor = None
def __enter__(self):
# open the database connection and create a cursor
self.connection = sqlite3.connect(self.db_file)
self.cursor = self.connection.cursor()
return self.cursor
def __exit__(self, exc_type, exc_value, traceback):
# commit any pending transactions
if exc_type is None:
self.connection.commit()
else:
# rollback in case of an error
self.connection.rollback()
# close the cursor and connection
self.cursor.close()
self.connection.close()
# usage of the custom context manager
db_file = 'test_student.db'
# create and populate the database for demonstration purposes
def setup_database():
with sqlite3.connect(db_file) as conn:
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
)
''')
cursor.execute('INSERT INTO users (name, email) VALUES (?, ?)', ('Alice', '[email protected]'))
cursor.execute('INSERT INTO users (name, email) VALUES (?, ?)', ('Bob', '[email protected]'))
# function to query the database
def query_users():
with DatabaseConnection(db_file) as cursor:
cursor.execute('SELECT * FROM users')
rows = cursor.fetchall()
for row in rows:
print(f"ID: {row[0]}, Name: {row[1]}, Email: {row[2]}")
setup_database()
print("User Records:")
query_users()