-
Notifications
You must be signed in to change notification settings - Fork 5
/
acrcloud_mysql.py
71 lines (64 loc) · 2.3 KB
/
acrcloud_mysql.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
import sys
import pymysql
import traceback
class MysqlManager:
def __init__(self, host, port, user, passwd, dbname):
self.host = host
self.port = port
self.user = user
self.passwd = passwd
self.dbname = dbname
self.conn = pymysql.connect(host=host, user=user,
passwd=passwd, db=dbname,
port=port, charset='utf8')
self.curs = self.conn.cursor()
def reconnect(self):
self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user,
passwd=self.passwd, db=self.dbname, charset='utf8')
self.curs = self.conn.cursor()
def commit(self):
try:
self.conn.commit();
except (AttributeError, pymysql.Error):
self.reconnect()
try:
self.conn.commit();
except pymysql.Error:
raise
def execute(self, sql, params=None):
self.conn.ping(reconnect=True)
if params:
try:
self.curs.execute(sql, params)
except (AttributeError, pymysql.Error):
self.reconnect()
try:
self.curs.execute(sql, params)
except pymysql.Error:
raise
else:
try:
self.curs.execute(sql)
except (AttributeError, pymysql.Error):
self.reconnect()
try:
self.curs.execute(sql)
except pymysql.Error:
raise
return self.curs
def executemany(self, sql, params):
if params:
try:
self.curs.executemany(sql, params)
except (AttributeError, pymysql.Error):
self.reconnect()
try:
self.curs.executemany(sql, params)
except pymysql.Error:
raise
return self.curs
def insert_results(self, params):
self.conn.ping(reconnect=True)
sql = '''insert into result_info (access_key, stream_url, stream_id, result, timestamp, catchDate) values (%s, %s, %s, %s, %s, %s) on duplicate key update id=LAST_INSERT_ID(id)'''
self.curs.execute(sql, params)
self.conn.commit()