import pymysql
class MysqlHandler:
#建立数据库连接,新建一个查询页面
def __init__(self):
# port要转为int
self.mysql = pymysql.connect(host=handler.get_option_value("mysql", "connection"),
user=handler.get_option_value("mysql", "user"),
password=handler.get_option_value("mysql", "password"),
port= int(handler.get_option_value("mysql", "port")), charset="utf8", autocommit=True)
self.cursor = self.mysql.cursor(pymysql.cursors.DictCursor) # fetchone 是字典 fetchall是list套dict
#获取查询结果,结果是dict,上面建立游标指定了是dict
def query_one(self,sql):
self.cursor.execute(sql)
data = self.cursor.fetchone()
return data
#关闭查询页面,关闭连接
def close(self):
self.cursor.close()
self.mysql.close()