-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabaseConn.py
160 lines (138 loc) · 5.39 KB
/
databaseConn.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
import json
import pymysql
import hosts
class MySqlHelper(object):
def __init__(self):
host = hosts.Hosts()
self.host = host.host
self.port = host.port
self.user = host.user
self.password = host.password
self.db = host.db
self.conn = None
self.cursor = None
def connect(self):
self.conn = pymysql.connect(host=self.host, port=self.port,
user=self.user, password=self.password, db=self.db)
self.cursor = self.conn.cursor()
def disconnect(self):
self.conn.close()
def get_num(self, table):
query = "select count(*) from {}".format(table)
self.cursor.execute(query)
data = self.cursor.fetchall()[0][0]
return data
def query_by_id(self, id, table):
try:
query = "select * from {} where id = {}".format(table, id)
self.cursor.execute(query)
data = self.cursor.fetchall()[0]
return data
except:
return None
# operations for TABLE guesses
def insert_into_guesses(self, novel_id, user_id, hasContext, guess, target_word, isRight):
try:
query = """INSERT INTO guesses VALUES ({}, {}, {}, "{}","{}", {})""".format(novel_id, user_id, int(hasContext), guess,target_word, int(isRight))
print("insertion query is:", query)
self.cursor.execute(query)
self.conn.commit()
except Exception as e:
print("Insertion into guesses failed:", e)
return "Insertion into guesses failed"
# operations for TABLE output_novels
def fetch_randomly(self):
# randomly fetch a record
# fetch records to be evaluated with context first, if not exist, fetch records to be evaluated without context
data = self.select_with_context()
if (data is not None):
return (data, True)
else:
return None
def random_select_with_context(self):
# randomly select a record to be evaluated with context
print("select with context")
try:
query = "SELECT * FROM output_novels WHERE hitTimesInContext<2 ORDER BY RAND() LIMIT 1"
self.cursor.execute(query)
data = self.cursor.fetchall()
if data:
return self.query_by_id(data[0][0], "novels")
else:
return None
except Exception as e:
print("Selection with context failed:", e)
return "Selection with context failed"
def select_with_context(self):
# randomly select a record to be evaluated with context
try:
query = "SELECT * FROM output_novels WHERE hitTimesInContext<1"
self.cursor.execute(query)
data = self.cursor.fetchall()
if data:
return self.query_by_id(data[0][0], "novels")
else:
return None
except Exception as e:
print("Selection with context failed:", e)
return "Selection with context failed"
def select_without_context(self):
# randomly select a record to be evaluated without context
print("select without context")
try:
# TODO: this query only fetches novels that passed the evaluation with context.
query = "SELECT * FROM output_novels WHERE hitTimesInContext<2 AND missTimesWithoutContext<10 ORDER BY RAND() LIMIT 1"
self.cursor.execute(query)
data = self.cursor.fetchall()
if data:
return self.query_by_id(data[0][0], "novels")
else:
return None
except Exception as e:
print("Selection without context failed:", e)
return "Selection without context failed"
def update_times_col(self, tabel, novel_id, col):
try:
query = "UPDATE {} SET {}={}+1 WHERE id={}".format(tabel, col, col, novel_id)
self.cursor.execute(query)
self.conn.commit();
except Exception as e:
print("Updating output_novels failed:", e)
return "Updating output_novels failed"
def update_islabeled(self, table, novel_id):
try:
query = "UPDATE {} SET is_labeled=1 WHERE novel_id={}".format(table,novel_id)
self.cursor.execute(query)
self.conn.commit();
except Exception as e:
print("Updating output_novels failed:", e)
return "Updating output_novels failed"
def delete_from_outputNovels(self, novel_id):
try:
query = "DELETE FROM output_novels WHERE id={}".format(novel_id)
self.cursor.execute(query)
self.conn.commit();
except Exception as e:
print("Deleting from output_novels failed:", e)
return "Deleting from output_novels failed"
def login_validation(self, user_id, pwd):
query = "SELECT * FROM users WHERE id={}".format(user_id)
self.cursor.execute(query)
data = self.cursor.fetchall()
if data:
if (data[0][1]==pwd):
return 0
else:
return 1
else:
return 2
if __name__ == '__main__':
tables = "novels"
sqlh = MySqlHelper()
sqlh.connect()
idx = sqlh.query_by_id(1000, tables)
print(idx)
idx = sqlh.query_by_id(999, tables)
print(idx)
sqlh.disconnect()
#print(d)