目前mysql的数据库管理工具在查看字段注释时无法像PL/SQL那么方便,mysql执行select * from table后,还得转到数据表的DDL页面查看字段注释,非常不方便,因此我用python写了个脚本,可以读取mysql数据表和字段注释并自动生成查询语句。
废话不多说,直接上代码。
import pymysql class GetMysqlTableComments(): def __init__(self, host, user, password, database,port,charset): self.db = pymysql.connect(host=host, user=user, password=password, port=port, database=database, charset=charset) self.cursor = self.db.cursor() def get_tables(self, database_name): sqlstr = '' # 查询mysql表名和注释 self.cursor.execute( 'select table_name,table_comment from information_schema.TABLES where TABLE_SCHEMA=%s order by table_name', database_name) return_tables = self.cursor.fetchall() for tabledata in return_tables: return_columns = self.get_columns(tabledata[0]) sqlstr = sqlstr +&#