mysql链接

# mysql链接
class LogMysql(object):
    conn = None
    cursor = None

    def __init__(self):
        # 生产
        self.conn = pymysql.connect(host='root', user='datacenter',
                                    password='111111',
                                    database='log', charset='utf8')
        # 本地测试
        # self.conn = pymysql.connect(host='192.168.10.5', user='root',
        #                             password='root',
        #                             database='unionlog', charset='utf8')
        self.cursor = self.conn.cursor()

    # 为了方便使用一般会选择将查询结果加上字段名称以字典组的方式返回查询结果
    def dict_fetchall(self):
        "Return all rows from a cursor as a dict"
        # 获取查询字段
        columns = [col[0] for col in self.cursor.description]
        # print(columns)
        return [dict(zip(columns, row)) for row in self.cursor.fetchall()]

    def get_table_list(self):
        # 判断表是否存在
        self.cursor.execute("SHOW TABLES")
        res = self.cursor.fetchall()
        table_list = []
        for i in res:
            table_list.append(i[0])
        # print("table_list", table_list)
        return table_list
原文地址:https://www.cnblogs.com/xiao-xue-di/p/12053120.html