python数据字典的定义和用法,Python 生成数据字典

代码

import pymysql as mysql

import sys

import getopt

def usage():

print(‘help:‘)

print(‘--host db server,default localhost‘)

print(‘--port db port,default 3306‘)

print(‘--user db username,default root‘)

print(‘--password db password,default blank‘)

print(‘--database db name‘)

print(‘--output markdown output file,default current path‘)

if __name__ == ‘__main__‘:

try:

opts, args = getopt.getopt(sys.argv[1:], "h",

["help", "host=", "port=", "database=", "user=", "password=", "output="])

except getopt.GetoptError:

sys.exit()

if ‘help‘ in args:

usage()

sys.exit()

print(opts)

host = "localhost"

user = input("帐号:")

password = input("密码:")

database = input("数据库名:")

port = 3306

mdfile = input("请输入生成的路径和文件名,默认当前目录下(./markdown.md):")

output = mdfile if mdfile else "./markdown.md"

for op, value in opts:

if op == ‘--host‘:

host = value

elif op == ‘--port‘:

port = value

elif op == ‘--database‘:

database = value

elif op == ‘--user‘:

user = value

elif op == ‘--password‘:

password = value

elif op == ‘--output‘:

output = value

elif op == ‘-h‘:

usage()

sys.exit()

if database == ‘‘:

usage()

# sys.exit()

conn = mysql.connect(host=host, port=port, user=user, password=password, database=‘information_schema‘)

cursor = conn.cursor()

cursor.execute(

"select table_name,table_comment from information_schema.tables where table_schema=‘%s‘ and table_type=‘base table‘" % database)

tables = cursor.fetchall()

markdown_table_header = """

### %s

#### %s

字段名 | 字段类型 | 默认值 | 注释

---- | ---- | ---- | ----

"""

markdown_table_row = """%s | %s | %s | %s

"""

f = open(output, ‘w‘)

for table in tables:

cursor.execute(

"select COLUMN_NAME,COLUMN_TYPE,COLUMN_DEFAULT,COLUMN_COMMENT from information_schema.COLUMNS where table_schema=‘%s‘ and table_name=‘%s‘" % (

database, table[0]))

tmp_table = cursor.fetchall()

p = markdown_table_header % table

for col in tmp_table:

p += markdown_table_row % col

f.writelines(p)

f.writelines(‘\r\n‘)

f.close()

print(‘generate markdown success!‘)

原文:https://www.cnblogs.com/jiangchunsheng/p/13405601.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值