-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLConnection.py
56 lines (39 loc) · 1.37 KB
/
SQLConnection.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
from PyQt4.QtSql import *
class SQLConnection:
"""Handles the conncetion to the SQL database"""
def __init__(self,path):
self.path = path
self.db = None
def open_database(self):
if self.db:
self.close_database()
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName(self.path)
opened_ok = self.db.open()
return opened_ok
def close_database(self):
if self.db:
if self.db.isOpen() == True:
self.db.close()
#remove the database from the QSqlDatabase object - "conn" is the default
#database name
QSqlDatabase.removeDatabase("conn")
closed = self.db.open()
print("closed con")
else:
print("No connection open")
else:
print("No connection to close!")
def closeEvent(self,event):
self.close_database()
def show_all_products(self):
query = QSqlQuery()
query.prepare(""" SELECT * FROM Product""")
query.exec_()
return query
def find_products_by_number(self,values):
query = QSqlQuery()
query.prepare(""" SELECT * FROM Product WHERE ProductID =? """)
query.addBindValue(values[0])
query.exec_()
return query