-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsection_03.py
More file actions
59 lines (48 loc) · 1.26 KB
/
section_03.py
File metadata and controls
59 lines (48 loc) · 1.26 KB
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
54
55
56
57
58
59
import sqlite3
# Project path
PROJECT_PATH='e:/FILES/projects/python-projects/sqlite_with_python/'
# Connect to dbase on disk
db = sqlite3.connect(PROJECT_PATH + 'customer.db')
# # Connect to dbase in-memory
# db = sqlite3.connect(':memory:')
# # Uncomment to show SQL commands as they are executed
# db.set_trace_callback(print)
# Create a cursor
cursor = db.cursor()
# # Uncomment to see table and row ids
# view_table = cursor.execute('SELECT rowid, * FROM customers;')
# print(view_table.fetchall())
# Select rows
tmp = cursor.execute('''
SELECT *
FROM customers
WHERE last_name LIKE "Ram%";
''')
rows = tmp.fetchall()
for row in rows:
print(row)
print()
# Select one
# Just returns the one item, not the cursor object
one_row = cursor.execute('''
SELECT *
FROM customers
WHERE last_name LIKE "Ram%";
''').fetchone()
print(one_row)
print()
# Select multiple
# Just returns the list, not the cursor object
many_rows = cursor.execute('''
SELECT *
FROM customers
WHERE last_name LIKE "Ram%";
''').fetchmany(3)
for row in many_rows:
print(row)
print()
# Only reads, no changes to commit
# # Commit the changes
# db.commit()
# Close the db connection
db.close()