-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdb.py
91 lines (74 loc) · 2.96 KB
/
db.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
import pymysql
import yaml
import datetime
# MySQL configurations
db = yaml.load(open('db.yaml'))
def getAllRecords():
try:
connection = pymysql.connect(host = db['mysql_host'],
user = db['mysql_user'],
password = db['mysql_password'],
db = db['mysql_db'],
cursorclass=pymysql.cursors.DictCursor,
use_unicode=True, charset="utf8")
cursor = connection.cursor()
resultVal = cursor.execute("SELECT * from nlpdb order by ID desc;")
print("rows:",resultVal)
if resultVal > 0:
records = cursor.fetchall()
print(records)
#connection.close()
return records
else:
print("Kayıt YOK!")
finally:
connection.close()
def addRecord(_UserInput,_PredictedText, _IP):
try:
connection = pymysql.connect(host = db['mysql_host'],
user = db['mysql_user'],
password = db['mysql_password'],
db = db['mysql_db'],
cursorclass=pymysql.cursors.DictCursor,
use_unicode=True, charset="utf8")
ID = 0
cursor = connection.cursor()
try:
sql = (
"INSERT INTO nlpdb (UserInputText, PredictedText, IsTrue, UserSuggestion, CreatedDate, LastModifiedDate, IP) "
"VALUES (%s, %s, %s, %s, %s, %s, %s)"
)
cursor.execute(sql, (str(_UserInput), str(_PredictedText), "-1", "", datetime.datetime.now(), datetime.datetime.now(), str(_IP)))
#connection.commit()
ID = cursor.lastrowid
#cursor.close()
except Exception as e:
print("error",str(e))
finally:
return ID
finally:
connection.close()
def updateRecord(_ID, _IsTrue, _UserSuggestion):
try:
connection = pymysql.connect(host = db['mysql_host'],
user = db['mysql_user'],
password = db['mysql_password'],
db = db['mysql_db'],
cursorclass=pymysql.cursors.DictCursor,
use_unicode=True, charset="utf8")
ID = 0
cursor = connection.cursor()
try:
sql = (
"UPDATE nlpdb SET IsTrue = %s, UserSuggestion=%s, LastModifiedDate=%s WHERE ID = %s"
)
cursor.execute(sql, (int(_IsTrue), str(_UserSuggestion), datetime.datetime.now(), int(_ID)))
#connection.commit()
#cursor.close()
ID = 1
except Exception as e:
print("error",str(e))
finally:
return ID
finally:
connection.close()