表中查询一个字段,该字段是个sql1,需要循环验证表1中的每一个sql1执行是否报错,报错则打印表中的序号和sql
1、查询得到这个sql
2、获取这个sql并执行
3、抛出异常
import sys import pymysql mysqlHost = "XXXXXXXXXX" mysqlPort = XXXXXXXXXX mysqlUser = "XXXXXXXXXX" mysqlPwd = "XXXXXXXXXX" mysqlSchema = "XXXXXXXXXX" db = pymysql.connect(host = mysqlHost,port = mysqlPort,user = mysqlUser,passwd = mysqlPwd,db = mysqlSchema,charset = 'utf8'); querySql = """SELECT sql字段,字段 FROM 表 """ querySqlCursor = db.cursor() querySqlCursor.execute(querySql) querySqlResult = querySqlCursor.fetchall() SqlCursor = db.cursor() # 踩过两个坑,第一个是sql获取的是元祖,所以即使只有一个字段也要索引sql[0] # 踩的第二坑就是sql中有换行,所以要替换为空格 for sql in querySqlResult: try: sql[0].replace('\r\n\t',' ') SqlCursor.execute(sql[0]) SqlResult = SqlCursor.fetchall() # print(SqlResult) except Exception as e: print("error %s" % e) print (sql) db.close()