-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor_db_query.py
63 lines (38 loc) · 1.3 KB
/
cursor_db_query.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
54
55
56
import datetime
import sqlite3
db = sqlite3.connect('employee.sqlite')
cursor = db.cursor()
for item in cursor.execute('SELECT * FROM salary'):
print(item)
print('=' * 40)
salary_records = cursor.execute('SELECT * FROM salary')
print(salary_records)
print(type(salary_records))
for item in salary_records:
print(item)
print('=' * 40)
# Warning: cursor is a generator so when iteration on cursor, it can not rest and print (you should call again cursor)
for item in salary_records:
print(item)
print('=' * 40)
# for read one record:
salary_records = cursor.execute('SELECT * FROM salary')
print(salary_records.fetchone())
print(salary_records.fetchone())
print(salary_records.fetchone())
print(salary_records.fetchone())
print('=' * 40)
# it is List of all records of salary:
salary_records = cursor.execute('SELECT * FROM salary')
print(salary_records.fetchall())
print('=' * 40)
cursor.execute('UPDATE salary SET amount=? WHERE id=?', (13000, 1003))
# would give you the number of results of a query:
print(cursor.rowcount)
salary_records = cursor.execute('SELECT * FROM salary')
print(cursor.rowcount)
print('=' * 40)
# would give you last record that changed
today = datetime.date.today()
cursor.execute('INSERT INTO salary VALUES(?, ?, ?)', (1004, 28000, today))
print(cursor.lastrowid)