-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbFacade.py
More file actions
100 lines (79 loc) · 2.27 KB
/
Copy pathdbFacade.py
File metadata and controls
100 lines (79 loc) · 2.27 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
89
90
91
92
93
94
95
96
import MySQLdb
import json
#current database by default
dataBase = "SQL"
class MongoDB():
'''for future purpose'''
pass
class SQL():
'''define our connection to SQL_DB and gives data to it as user object'''
def check(self):
'''for test case'''
print("SQL")
def connectSQL_DB(self):
'''creating DB connect'''
connect = MySQLdb.connect(host='localhost', user='root', passwd='', db='test_project')
return connect
def update(self, data):
'''updating record'''
conn = self.connectSQL_DB()
cursor = conn.cursor()
cursor.execute("UPDATE {0} SET name='{2}', surname='{3}', age={4} WHERE id={1}".format(data.table, data.id, data.name, data.surname, data.age))
print('DB updated')
conn.commit()
conn.close()
def select(self, data):
'''selecting record '''
conn = self.connectSQL_DB()
cursor = conn.cursor()
cursor.execute("SELECT * from {0} WHERE id={1}".format(data.table, data.id))
print('DB selected')
row = cursor.fetchall()
print(row)
open("jsondata.json", 'a').close()
somedict = {
"table" : "user",
"fields" : {
"id" : "5",
"name" : "Robby",
"surname" : "Murrey",
"age" : "50"
},
"method" : "select"
}
with open("jsondata.json", 'w') as d:
json.dump(somedict, d)
conn.close()
def delete(self, data):
'''deleting record '''
conn = self.connectSQL_DB()
cursor = conn.cursor()
cursor.execute("DELETE from {0} where id={1}".format(data.table, data.id))
conn.commit()
print('record deleted')
conn.close()
def insert(self, data):
'''inserting record '''
conn = self.connectSQL_DB()
cursor = conn.cursor()
cursor.execute("INSERT INTO {0} (id, name, surname, age) VALUES ({1}, '{2}', '{3}', {4})".format(data.table, "DEFAULT", data.name, data.surname, data.age))
conn.commit()
print('record inserted')
conn.close()
def run(self, data):
'''check out action method with database'''
self.method = data.method
if self.method == "update":
self.update(data)
elif self.method == "select":
self.select(data)
elif self.method == "delete":
self.delete(data)
elif self.method == "insert":
self.insert(data)
else: print("Methot not defined...")
class Facade():
'''DB choise'''
def dbChoise(self):
if dataBase == "SQL": return SQL()
if dataBase == "MongoDB" : return Mongo()