欢迎访问 夜阑小雨 我的学习碎片档案,这里记录了我的学习内容和工作中经验,希望给您带去帮助。

mysql导出数据字典到word

PHP 夜阑小雨 3539℃ 0评论

使用navicat导出数据字典,只能一张一张表的操作。

有点麻烦,于是想了这个办法,写个简单的Python导出数据库的数据字典。

将以下代码保存为 doc.py,修改对应的链接信息

#coding:utf-8
import pymysql
from docx import Document
connection=pymysql.connect(host=’127.0.0.1′,
user=’root’,
password=’root’,
db=’information_schema’,
port=3306,
charset=’utf8′)#information_schema 这个不能变,其他是你的数据库的连接信息
schema=’shop’#要导出的数据库的名字
cursor=connection.cursor()
sql=”select table_name,table_comment from information_schema.tables where TABLE_SCHEMA = ‘”+str(schema)+”‘”
cursor.execute(sql)
tableInfoList=cursor.fetchall()
doc=Document()
for tableInfo in tableInfoList:
tableName=tableInfo[0]
tableComment=tableInfo[1]
table_explain = “表名:”+tableName+” ( “+tableComment+” ) ”
tableInfoSql=”SELECT C.COLUMN_NAME AS ‘字段名’,C.COLUMN_TYPE AS ‘数据类型’,C.IS_NULLABLE AS ‘允许为空’,C.EXTRA AS ‘PK’,C.COLUMN_COMMENT AS ‘字段说明’ FROM information_schema.COLUMNS C INNER JOIN TABLES T ON C.TABLE_SCHEMA = T.TABLE_SCHEMA AND C.TABLE_NAME = T.TABLE_NAME WHERE T.TABLE_SCHEMA = ‘”+str(schema)+”‘ and T.TABLE_NAME='”+str(tableName)+”‘”
cursor.execute(tableInfoSql)
tableColumnInfoList = cursor.fetchall()
p = doc.add_paragraph(”)
p.add_run(table_explain, style=”Heading 1 Char”)
row=cursor.rowcount
table=doc.add_table(rows=1,cols=5)
table.style = ‘TableGrid’
hdr_cells = table.rows[0].cells
hdr_cells[0].text=’字段名’
hdr_cells[1].text = ‘字段类型’
hdr_cells[2].text = ‘允许为空’
hdr_cells[3].text = ‘PK’
hdr_cells[4].text = ‘字段说明’
for tableColumn in tableColumnInfoList:
new_cells = table.add_row().cells
new_cells[0].text=tableColumn[0]
new_cells[1].text = tableColumn[1]
new_cells[2].text = tableColumn[2]
new_cells[3].text = tableColumn[3]
new_cells[4].text = tableColumn[4]
p = doc.add_paragraph(”)
doc.save(‘D:/sqlword.docx’)

导出的docx的样子是

 

下载文件:导出mysql数据字典

转载请注明:夜阑小雨 » mysql导出数据字典到word

喜欢 (34)or分享 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(1)个小伙伴在吐槽
  1. 不错学习了
    夜阑小雨2020-01-21 13:32 回复