1 import pymysql
2
3 db = pymysql.connect(
4 host='127.0.0.1',
5 db='diag',
6 user='root',
7 passwd='root',
8 charset='utf8',
9 use_unicode=True
10 )
11
12 cursor = db.cursor()
13
14 sql = 'show tables from diag'
15 rows = cursor.execute(sql) # 返回执行成功的结果条数
16 print(f'一共有 {rows} 张表')
17 for d in cursor.fetchall():
18 name: object = d[0]
19 # print(name)
20 if "msg" in name:
21 sql = "DROP TABLE IF EXISTS {}".format(name)
22 try:
23 cursor.execute(sql)
24 print('table:{}--已被删除'.format(name))
25 except Exception as e:
26 print(e)
27