python pymysql 刪除指定數據庫中的數據表


 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   

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM