-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.py
130 lines (103 loc) · 3.68 KB
/
Functions.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# IMPORTS
import os
import sys
# Import SQLite
import sqlite3
from sqlite3 import Error
# Import QTableWidgetItem (For creating new table cells)
from PySide2.QtWidgets import QTableWidgetItem
#Functions
class AppFunctions():
def __init__(self, arg):
super(AppFunctions, self).__init__()
self.arg = arg
# Create DataBase connection
def create_connection(db_file):
""" Create a database connection to a SQLite database"""
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
# Create Table
def create_table(conn, create_table_sql):
try:
c = conn.cursor()
c.execute(create_table_sql)
except Error as e:
print(e)
# Main function
def main(dbFolder):
# Create table if it does not exist
create_user_table = """
CREATE TABLE IF NOT EXISTS Users (
USER_ID INTEGER PRIMARY KEY AUTOINCREMENT,
USER_NAME TEXT, USER_EMAIL TEXT, USER_PHONE TEXT
);
"""
conn = AppFunctions.create_connection(dbFolder)
# create tables
if conn is not None:
# create user table
AppFunctions.create_table(conn, create_user_table)
else:
print("Error! cannot create the database connection!")
# Get all users from database
def getAllUsers(dbFolder):
# Create db connection
conn = AppFunctions.create_connection(dbFolder)
get_all_users= """
SELECT * FROM Users
"""
try:
c = conn.cursor()
c.execute(get_all_users)
return c
except Error as e:
print(e)
# Add a user to db
def addUser(self, dbFolder):
# Create db connection
conn = AppFunctions.create_connection(dbFolder)
# Get form values
userName = self.ui.userName.text()
email = self.ui.email.text()
phoneNo = self.ui.phoneNo.text()
# Create sql statement
insert_person_data_sql = f"""
INSERT INTO Users (USER_NAME, USER_EMAIL, USER_PHONE) VALUES ('{userName}', '{email}', '{phoneNo}');
"""
# Execute sql statement
if not conn.cursor().execute(insert_person_data_sql):
print('Could not insert person data')
else:
conn.commit()
# Clear form input
self.ui.userName.setText("")
self.ui.email.setText("")
self.ui.phoneNo.setText("")
# Load new user from DB to table view
AppFunctions.displayUsers(self,AppFunctions.getAllUsers(dbFolder))
# Display Users
def displayUsers(self, rows):
# Create new row
for row in rows:
# Get number of rows
rowPosition = self.ui.tableWidget.rowCount()
# Skip rows that have already been loaded to table
if rowPosition+1 > row[0]:
continue
itemCount = 0
# Create new table row
self.ui.tableWidget.setRowCount(rowPosition+1)
qtablewidgetitem = QTableWidgetItem()
self.ui.tableWidget.setVerticalHeaderItem(rowPosition, qtablewidgetitem)
# Add items to row
for item in row:
self.qtablewidgetitem = QTableWidgetItem()
self.ui.tableWidget.setItem(rowPosition, itemCount, self.qtablewidgetitem)
self.qtablewidgetitem = self.ui.tableWidget.item(rowPosition, itemCount)
self.qtablewidgetitem.setText(str(item))
itemCount = itemCount+1
rowPosition = rowPosition+1