-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrud.py
66 lines (48 loc) · 1.76 KB
/
Crud.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
from Connection import Connection
class Crud:
def insert(self, table, values):
conn = Connection().conn()
cursor = conn.cursor()
sql = "INSERT INTO %s (%s)"
cursor.execute(sql, (table, values))
conn.commit()
lastid = cursor.lastrowid
cursor.close()
conn.close()
return lastid
def getAll(self, table):
conn = Connection().conn()
cursor = conn.cursor()
sql = "SELECT * FROM %s"
cursor.execute(sql, (table, ))
result = cursor.fetchall()
cursor.close()
conn.close()
return result
def findById(self, table, id):
conn = Connection().conn()
cursor = conn.cursor()
sql = "Select * FROM {table} WHERE id = {id}".format(table = table, id = id)
cursor.execute(sql)
result = cursor.fetchone()
cursor.close()
conn.close()
return result
def findBy(self, table, colunm, value, condition="="):
conn = Connection().conn()
cursor = conn.cursor()
sql = "SELECT * FROM {table} WHERE {colunm} {condition} '{value}'".format(table = table, colunm = colunm, condition = condition, value = value)
# cursor.execute("SELECT * FROM %s WHERE %s %s %s", (table, colunm, condition, value))
cursor.execute(sql)
result = cursor.fetchall()
cursor.close()
conn.close()
return result
def delete(self, table, id):
conn = Connection().conn()
cursor = conn.cursor()
sql = "DELETE {table} WHERE id = {id}".format(table = table, id = id)
cursor.execute(sql)
cursor.close()
conn.close()
return "item {id} removed of {table}".format(table = table, id = id)