mysql link

# 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 ' the root ' , 
        # password = ' the root ' , 
        # Database = ' unionlog ' , charset = ' UTF8 ' ) 
        self.cursor = self.conn.cursor () 

    # for ease of use will generally choose to add the results of the query field name dictionary set in the manner of returning query results 
    DEF dict_fetchall (Self): 
        " the return All rows from AS a dict a Cursor " 
        # Get query field 
        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

Guess you like

Origin www.cnblogs.com/xiao-xue-di/p/12053120.html