-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore.py
45 lines (37 loc) · 1.35 KB
/
score.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
import MySQLdb
class Score:
def __init__(self, host, user, passwd, db):
self.db = db
self.host = host
self.passwd = passwd
self.user = user
def query_mysql(self, query):
db = MySQLdb.connect(host=self.host, user=self.user,
passwd=self.passwd, db=self.db)
cur = db.cursor()
try:
cur.execute(query)
db.commit()
if int(cur.rowcount) == 1:
return cur.fetchone()[0]
else:
return cur.fetchall()
except:
db.rollback()
def create_user(self, user):
find_user = """SELECT user from score where user = '%s'""" % user
if not self.query_mysql(find_user):
sql = """INSERT INTO score (user) VALUES ('%s')""" % user
self.query_mysql(sql)
def increment(self, user):
self.create_user(user)
sql = """UPDATE score SET count = count + 1 WHERE user = '%s'""" % user
self.query_mysql(sql)
def decrement(self, user):
self.create_user(user)
sql = """UPDATE score SET count = count - 1 WHERE user = '%s'""" % user
self.query_mysql(sql)
def get_score(self, user):
self.create_user(user)
sql = """SELECT count from score where user = '%s'""" % user
return self.query_mysql(sql)