-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
78 lines (61 loc) · 2.02 KB
/
model.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
import pymysql
# --- Config ---
_host = "localhost"
_user = "root"
_pass = ""
_database = "db_global_test"
_table = "tbl_lorem"
class Database:
def connect(self):
return pymysql.connect(host=_host, user=_user, password=_pass, database=_database, charset='utf8mb4')
def read(self, id):
con = Database.connect(self)
cursor = con.cursor()
try:
if id == None:
cursor.execute("SELECT * FROM tbl_lzw order by id asc")
else:
cursor.execute("SELECT * FROM tbl_lzw where id = %s order by id asc", (id,))
return cursor.fetchall()
except:
return ()
finally:
con.close()
def insert(self, data):
con = Database.connect(self)
cursor = con.cursor()
try:
cursor.execute("INSERT INTO tbl_lzw(file_name,file_size) VALUES(%s, %s)",
(data['file_name'], data['file_size']))
con.commit()
return True
except:
con.rollback()
return False
finally:
con.close()
def update(self, id, data):
con = Database.connect(self)
cursor = con.cursor()
try:
cursor.execute("UPDATE tbl_user set nama = %s, nik = %s, alamat = %s, jk = %s, hari = %s, tanggal = %s, tempat = %s, penyebab = %s where id = %s",
(data['nama'], data['nik'], data['alamat'], data['jk'], data['hari'], data['tanggal'], data['tempat'], data['penyebab'], id,))
con.commit()
return True
except:
con.rollback()
return False
finally:
con.close()
def delete(self, name):
con = Database.connect(self)
cursor = con.cursor()
try:
cursor.execute("DELETE FROM tbl_lzw where file_name = %s", (name))
con.commit()
return True
except:
con.rollback()
return False
finally:
con.close()