1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python查询mysql导出数据字典

python查询mysql导出数据字典

时间:2019-08-03 10:54:29

相关推荐

python查询mysql导出数据字典

连接数据库查询,执行后自动生成Markdown文档,根据自己的需要添加删除所需字段

import pymysql, importlib, sys, timedef generate(host, port, username, password, database_name, time):"""生成数据库字典表"""importlib.reload(sys)conn = pymysql.connect(host=host, port=port, user=username, passwd=password, database=database_name)cursor = conn.cursor()cursor.execute("SELECT TABLE_NAME, TABLE_COMMENT FROM information_schema.TABLES WHERE table_type='BASE TABLE' AND TABLE_SCHEMA='%s'" % database_name)tables = cursor.fetchall()markdown_table_header = """\n\n\n### %s (%s) \n| 序号 | 字段名称 | 数据类型 | 是否为空 | 字段说明 |\n| :--: |----| ---- | ---- | ---- |\n"""markdown_table_row = """| %s | %s | %s | %s | %s |"""f = open(database_name + time + '.md', 'w')for table in tables:cursor.execute("SELECT ORDINAL_POSITION, COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_COMMENT ""FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='%s' AND TABLE_NAME='%s' ORDER BY ORDINAL_POSITION" % (database_name, table[0]))tmp_table = cursor.fetchall()p = markdown_table_header % (table[0], remove_newline(table[1]))for col in tmp_table:p += (remove_newline(markdown_table_row % col) + "\n")print(p)f.writelines(p)f.close()def remove_newline(text):"""去除文本中的换行符号"""return text.replace("\r", "").replace("\n", "")

调用情况如下

执行结果如下

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。