-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqlController.py
189 lines (163 loc) · 7.84 KB
/
sqlController.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import mysql.connector
#attributes needed for the specification of columns in a table
columns = {
"CustomerCreditCard": ("(", "acc_id,", "card_num",")"),
"CustomerAccount": ("(", "acc_id," ,"email,","password,","fname,","lname,", "gender,","date_of_birth,", "street,", "city,", "parish,", "telephone,","created_on" ")"),
"CreditCardDetails":("(","card_num,", "expiration_month,","expiration_year", "billing_street,", "billing_city,", "billing_parish",")"),
"Branch":("(","br_id,", "name,","street,", "city,", "parish,", "telephone",")"),
"Laptop":("(","serial_num,", "name,", "model,", "brand,", "description,", "picture,","price",")"),
"PurchaseItems":("(","pur_id,","acc_id,", "serial_num,","br_id,", "quantity,", "cost,", "date_purchased", ")"),
"CustomerCart":("(","acc_id,", "item_count,","value", ")"),
"CartItems": ("(", "acc_id,", "serial_num,", "br_id,", "quantity,", "cost,", "purchasing,", "date_added", ")"),
"Receipt":("(","track_num,", "invoice", ")"),
"Checkout": ("(", "acc_id,", "track_num,", "total_cost,", "transaction_date", ")"),
"WriteReview":("(","acc_id,","serial_num,", "rev_text,", "date_written", ")"),
"Warehouse":("(","wh_id,", "street,", "city,", "parish,", "telephone", ")"),
"Stores":("(","wh_id,","serial_num,", "quanitity", ")"),
"ItemInStock": ("(", "serial_num,", "quantity", ")"),
"ItemSold": ("(", "serial_num,", "amount", ")")
}
class databaseGenerator:
def __init__(self, dbname, columns):
self.dbname = dbname
self.column = columns
self.mydb = mysql.connector.connect(host="localhost", user="root", passwd="", database=dbname)
self.mycursor = self.mydb.cursor()
# attrib is a list of the arributes for the record,
# table is the string with the name of the table
#NB: if the datatype in the db is varchar the arrib
#value in the list should be represented as a quoted string
def addRecord(self, attrib, name):
try:
strstatement =""
strstatement ="INSERT INTO "+str(name)+" "+ "".join(self.column[name])
strstatement += ' VALUES({});'.format(','.join("{0}".format(x) for x in attrib))
self.mycursor.execute(strstatement)
except:
print("Please check input for addRecord")
#key is the identifier of the value to be removed
#name is the name of the table to removed the record form
#coltocompare is the name of the column to compare primkey against
#NB: primkey must be a quoted string if datatype is varchar
def removeRecord(self, key, name, coltocompare):
try:
strstatement = ""
strstatement = "DELETE FROM "+str(name)
strstatement += " WHERE {} = {};".format(coltocompare, key)
self.mycursor.execute(strstatement)
except:
print("Please check input for removeRecord")
#name of table must be python string
def showTableAll(self, tableName):
try:
strstatement = ""
strstatement = "SELECT * FROM {};".format(tableName)
self.mycursor.execute(strstatement)
records = self.mycursor.fetchall()
return records
except:
print("Please check input for showTableAll")
#key must be quoted string if the datatype in the database is varchar or date
#tableName - the name of the table to be updated
# columntoupdate - name of column to be updated
# selection - is the column to be selected
#key - the unique identifier of the record
#NT
def showTableCondition(self, tableName, coltocompare, key, selection):
try:
strstatement = ""
strstatement = "SELECT {} FROM {} WHERE {} = {};".format(selection, tableName, coltocompare, key)
self.mycursor.execute(strstatement)
records = self.mycursor.fetchall()
return records
except:
print("Please check input for showTableCondition")
#key must be quoted string if the datatype in the database is varchar or date
#tableName - the name of the table to be updated
# columntoupdate - name of column to be updated
# value - is the new value of the column
#key - the unique identifier of the record
#idfn - the column to compare to key in order to find specified record
def updateRecord(self, tableName, columntoupdate, key, value, idfn ):
try:
strstatement = ""
strstatement = "UPDATE {} SET {} = {} WHERE {} = {};".format(tableName, columntoupdate, value, idfn, key )
self.mycursor.execute(strstatement)
except:
print("Pease check input for updateRecord")
#tableName - the name of the table to be updated
# column - name of column to be updated
# value - is the new value of the column
def updateTable(self, tableName, column, value):
try:
strstatement = ""
strstatement = "UPDATE {} SET {} = {};".format(tableName, column, value)
self.mycursor.execute(strstatement)
except:
print("Please check input for updateTable")
def orderByPrice(self, ordr):
try:
strstatement = "CALL orderByPrice(\"{}\");".format(ordr)
self.mycursor.execute(strstatement)
records = self.mycursor.fetchall()
return records
except:
print("Please check input data type for orderByPrice")
def getByName(self, name):
try:
strstatement = "CALL getByName(\"{}\");".format(name)
self.mycursor.execute(strstatement)
records = self.mycursor.fetchall()
return records
except:
print("Please check input data type for getByName")
def getByModel(self, model):
try:
strstatement = "CALL getByModel(\"{}\");".format(model)
self.mycursor.execute(strstatement)
records = self.mycursor.fetchall()
return records
except:
print("Please check input data type for getByModel")
def getByBrand(self, brand):
try:
strstatement = "CALL getByBrand(\"{}\");".format(brand)
self.mycursor.execute(strstatement)
records = self.mycursor.fetchall()
return records
except:
print("Please check input data type for getByBrand")
def getBranchByCount(self, column, colkey, condition):
try:
strstatement = "SELECT COUNT(*) FROM {} WHERE {} LIKE '%{}%';".format(column, colkey, condition)
self.mycursor.execute(strstatement)
records = self.mycursor.fetchall()
return records
except mysql.connector.Error as error:
print("Please check getBranchByCount {}".format(error))
def getBranchCount(self, column, colkey):
try:
strstatement = "SELECT COUNT(*) FROM {};".format(column, colkey)
self.mycursor.execute(strstatement)
records = self.mycursor.fetchall()
return records
except mysql.connector.Error as error:
print("Please check getBranchByCount {}".format(error))
#destructor closes database and connection
def __del__(self):
self.mycursor.close()
self.mydb.close()
if __name__ == "__main__":
db = None
db = databaseGenerator("compustore", columns)
#testing code
#tb = db.showTableCondition("test", "Username", "\"popii\"", "Password")
#print(tb)
#t = db.showTableAll("test")
#print(t)
#db.updateRecord("managers", "person_name", "\"john\"","\"Grim\"", "person_name")
#db.updateTable("test", "id", "1234*2")
#db.addRecord(["\"1234\"", "\"hviujk\"", "\"gdfku\"","\"hjfgf\""], "test")
#db.addRecord(["\"john\"", "\"samuels\""], "managers")
#n.addRecord([7564, 657437,], "creditcard")
#db.removeRecord("\"Luke\"", "managers", "manager_name")