#coding=utf-8
importpymysqlimportxlwtimportconfigparser#獲取數據庫連接
python基礎教程?defconn(sql, hostname, username, password, database):#打開數據庫連接
db = pymysql.connect(hostname, username, password, database, charset='utf8')#使用 cursor() 方法創建一個游標對象 cursor
cursor =db.cursor()#使用 execute() 方法執行 SQL 查詢
python爬蟲教程,cursor.execute(sql)#使用 fetchall() 方法獲取所有數據.
results =cursor.fetchall()#關閉數據庫連接
db.close()returnresultsdefwrite_excel(f, data, row):#創建excel對象
python游戲代碼,book =xlwt.Workbook()#添加一個表
sheet = book.add_sheet('表結構清單')#行計數
c =0#取出data中的每一個元組存到表格的每一行
python自學入門,for d indata:#將每一個元組中的每一個單元存到每一列
for index inrange(len(d)):#寫表頭
if c ==0:
代碼編程,sheet.write(c, index, row[index])continuesheet.write(c, index, d[index])
c+= 1
#保存excel
book.save(f)if __name__ == "__main__":
config=configparser.ConfigParser()
config.read("config.ini")
host= config.get("mysql", "host")
user= config.get("mysql", "user")
pwd= config.get("mysql", "password")
db= config.get("mysql", "database")
file= config.get("file", "file_name")
s= """SELECT
a.Table_name AS "表名",
a.Table_comment AS "表注釋",
b.Ordinal_position AS "序號",
b.Column_name AS "列名",
b.Data_type AS "列類型",
b.Column_comment AS "列注釋"
FROM
information_schema.TABLES a
JOIN information_schema.COLUMNS b ON a.Table_name = b.Table_name
WHERE
a.TABLE_SCHEMA = '{}'
AND a.Table_type = 'BASE TABLE'""".format(db)
row_name= ("表名", "表注釋", "序號", "列名", "列類型", "列注釋")#獲取表結構數據
table_schema =conn(s, host, user, pwd, db)
write_excel(file, table_schema, row_name)print("success!")
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态