-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMysqlTool.py
56 lines (51 loc) · 2 KB
/
MysqlTool.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
# -*- coding:utf-8 -*-
import MySQLdb
class MysqlTool:
def __init__(self, db_name, db_ip, db_port, db_user, db_password):
self.__db_name = db_name
self.__db_ip = db_ip
self.__db_port = db_port
self.__db_user = db_user
self.__db_password = db_password
self.__db = MySQLdb.connect(host=db_ip, port=db_port, user=db_user, passwd=db_password, db=db_name)
self.__cursor = self.__db.cursor()
self.__db.set_character_set('utf8')
def __del__(self):
self.__db.close()
def getLength(self, table, date):
query_sql = 'select count(*) from %s where trade_dt >= %s ;'
try:
self.__cursor.execute(query_sql % (table, date))
results = self.__cursor.fetchall()
return results[0][0]
except MySQLdb.Error as e:
try:
print("Error %d:\n%s" % (e.args[0], e.args[1]))
except IndexError:
print("MySQL Error:%s" % str(e))
self.__db.rollback()
def insert(self, table ,dataList):
insert_sql = 'insert into %s'% table + ' values(%s);'
try:
argList = ','.join(['%s'] * len(dataList))
self.__cursor.execute(insert_sql % argList, dataList)
self.__db.commit()
except MySQLdb.Error as e:
try:
print("Error %d:\n%s" % (e.args[0], e.args[1]))
except IndexError:
print("MySQL Error:%s" % str(e))
self.__db.rollback()
def selectAll(self, table):
query_sql = 'select * from %s;' % (table)
try:
self.__cursor.execute(query_sql)
results = self.__cursor.fetchall()
print(results)
return results
except MySQLdb.Error as e:
try:
print("Error %d:\n%s" % (e.args[0], e.args[1]))
except IndexError:
print("MySQL Error:%s" % str(e))
self.__db.rollback()