添加注释时,先运行,之后再用这个顺序v注释,不要乱顺序
SELECT COLUMN NAME,column comment from information schema. COLUMNS WHERE TABLE NAME ='ods yb csj abo7' and table schema= 'ods jw qxsj;
import pandas as pd
import numpy as np
from pymysql import connect
#链接数据库
conn = connect (host='10.10.169.141',user='***', password='***', charset='utf8')
#创建游标
cursor=conn.cursor()
pd.set_option ('display.max_columns', None)#设值显示最大列
来自网络查看表注释代码与Python批量无关
#可以在sqL中运行查看注释情况,也可以用来制作注释排序
SELECT
concat(
'alter table ',
table_schema, '.', table_name,
' modify column ', column_name, ' ', column_type, ' ',
if(is_nullable = 'YES', ' ', 'not null '),
if(column_default IS NULL, '',
if(
data_type IN ('char', 'varchar')
OR
data_type IN ('date', 'datetime', 'timestamp') AND column_default != 'CURRENT_TIMESTAMP',
concat(' default ''', column_default,''''),
concat(' default ', column_default)
)
),
if(extra is null or extra='','',concat(' ',extra)),
' comment ''', column_comment, ''';'
) '组合语句'
FROM information_schema.columns
WHERE table_schema = 'test_1' -- 库名
AND table_name = 'h_test' -- 表名
dict_1={'BID EVALUATION EXPERT':[”标段(包)编号,
"数据时间戳",
'专家姓名',
'专家类别',
'专家身份证件号是否为评标组长',
'Row ID',
'RorGuid',
'招标项目编号']}
for k in dict_1.keys():
print (k)
sq1_2="""SELECT
concat(
'alter table ',
table_schema, '.', table_name,
' modify column ', column_name, ' ', column_type, ' ',
if(is_nullable = 'YES', ' ', 'not null '),
if(column_default IS NULL, '',
if(
data_type IN ('char', 'varchar')
OR
data_type IN ('date', 'datetime', 'timestamp') AND column_default != 'CURRENT_TIMESTAMP',
concat(' default ''', column_default,''''),
concat(' default ', column_default)
)
),
if(extra is null or extra='','',concat(' ',extra)),
' comment ''', column_comment, ''';'
) '组合语句'
FROM information_schema.columns
WHERE table_schema = 'test_1' -- 库名
AND table_name = '%s';"""%k -- 表名
print((sql_2)
#构造sQl语句
df2= pd.read_sql(sql_2,conn)
#print (df2)
sql_list=[]
for index, row in df2.iterrows():
#print (index)
#print (row)
#将单引号中的值进行替换,按照宇典的键值+索引的方式进行替换
sql——s = list (row.str.replace("''","'%s'"%dict_1[k][indexl))
print (sql_s)
#将构造好的sa1语句加载到sg1的列表中
sql_list.extend(sgl_s)
#提交给数据库
for i in sql_list:
cursor.execute(i)
conn.commit
cursor.close()#关闭游标