给MySQL批量添加注释

添加注释时,先运行,之后再用这个顺序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()#关闭游标

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值