-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.py
More file actions
88 lines (77 loc) · 1.92 KB
/
Copy pathqueries.py
File metadata and controls
88 lines (77 loc) · 1.92 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import sys
from PyQt6.QtSql import QSqlDatabase, QSqlQuery
# Create the connection
con = QSqlDatabase.addDatabase("QSQLITE")
con.setDatabaseName("employees.sqlite")
# Open the connection
if not con.open():
print("Database Error: %s" % con.lastError().databaseText())
sys.exit(1)
# Create a query and execute it right away using .exec()
createTableQuery = QSqlQuery()
createTableQuery.exec(
"""
CREATE TABLE employees (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL,
name VARCHAR(40) NOT NULL,
job VARCHAR(50),
email VARCHAR(40) NOT NULL
)
"""
)
print(con.tables())
createTableQuery.exec(
"""
CREATE TABLE payroll (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL,
name VARCHAR(40) NOT NULL,
hourlyPay,
hoursWorked REAL NOT NULL
)
"""
)
print(con.tables())
insertQuery = QSqlQuery()
insertQuery.prepare(
"""
INSERT INTO employees (
name,
job,
email
)
VALUES (?, ?, ?)
"""
)
insertQueryPayRoll = QSqlQuery()
insertQueryPayRoll.prepare(
"""
INSERT INTO payroll (
name,
hourlyPay,
hoursWorked
)
VALUES (?, ?, ?)
"""
)
data = [
("Joe", "Senior Web Developer", "joe@example.com"),
("Lara", "Project Manager", "lara@example.com"),
("David", "Data Analyst", "david@example.com"),
("Jane", "Senior Python Developer", "jane@example.com"),
]
data_payroll = [
("Lara", "18", "10"),
("David", "20", "40"),
("Craig", "15", "30"),
("Craig", "15", "30"),
]
for name, job, email in data:
insertQuery.addBindValue(name)
insertQuery.addBindValue(job)
insertQuery.addBindValue(email)
insertQuery.exec()
for name, hourlyPay, hoursWorked in data_payroll:
insertQueryPayRoll.addBindValue(name)
insertQueryPayRoll.addBindValue(hourlyPay)
insertQueryPayRoll.addBindValue(hoursWorked)
insertQueryPayRoll.exec()