You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.1 KiB

import pymysql.cursors
class MySQLHelper:
# 配置
user = 'root'
password = 'DsideaL147258369'
host = '10.10.14.230'
port = 22066
db = 'huanghai'
def __init__(self, host=host, port=port, db=db, user=user, password=password, charset="utf8"):
self.host = host
self.user = user
self.port = port
self.password = password
self.charset = charset
self.db = db
try:
self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.password,
db=self.db, charset=self.charset, cursorclass=pymysql.cursors.DictCursor)
self.cursor = self.conn.cursor()
# 设置执行时间为8小时
sql = "set session wait_timeout=288000"
self.cursor.execute(sql)
sql = "set session interactive_timeout=288000"
self.cursor.execute(sql)
except Exception as e:
print('MySql Error : %d %s' % (e.args[0], e.args[1]))
def query(self, sql: object) -> object:
try:
self.cursor.execute(sql)
result = self.cursor.fetchall()
return result
except Exception as e:
print('MySql Error: %s SQL: %s' % (e, sql))
def execute(self, sql):
try:
self.cursor.execute(sql)
self.conn.commit()
# 获取更新了多少条数据
return self.cursor.rowcount
except Exception as e:
print('MySql Error: %s SQL: %s' % (e, sql))
def executeWithPara(self, sql, params):
try:
self.cursor.execute(sql, params)
self.conn.commit()
except Exception as e:
print('MySql Error: %s SQL: %s' % (e, sql))
def executemany(self, sql, data):
try:
self.cursor.executemany(sql, data)
self.conn.commit()
except Exception as e:
print('MySql Error: %s SQL: %s' % (e, sql))
def close(self):
self.cursor.close()
self.conn.close()