Python 查询mysql数据库

2022/4/2 19:19:40

本文主要是介绍Python 查询mysql数据库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

import pandas
import pymysql
import config
import pandas as pd
from  pandas import DataFrame
import openpyxl
def gogo():
    writer = pd.ExcelWriter('./结果/jieguo.xlsx')

    conn = pymysql.connect(host='192.168.140.97', port=3306, user='root', passwd='Huawei@imoc123', db='eticketdb',
                           charset='utf8')

    cursor = conn.cursor()

    for i in config.lst1:
        sql1 = """
        SELECT t.TABLE_NAME,t.table_comment,c.COLUMN_NAME,c.COLUMN_COMMENT
        FROM information_schema.`COLUMNS` c
        INNER JOIN information_schema.`TABLES` t
        ON c.TABLE_NAME = t.TABLE_NAME
        AND t.table_schema like 'easyopsdb'
        AND C.TABLE_NAME='%s'
        
        """%i
        # print(sql1)
        effect_row = cursor.execute(sql1)
        re_1 = cursor.fetchall()
        # print(type(re_1))
        dt = DataFrame(re_1)
        dt.columns = ["TABLE_NAME","table_comment","COLUMN_NAME","COMLUMN_COMMENT"]
        print(dt)
        dt.to_excel(writer, sheet_name=i, index=False, startrow=1)
        writer.save()
    cursor.close()
    conn.close()
    writer.close()
try:
    gogo()
except:
    Exception()


这篇关于Python 查询mysql数据库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程