import mysql.connector import sys user = ‘root‘ pwd = ‘123456‘ host = ‘localhost‘ db = ‘magento‘ conn = mysql.connector.connect(user=user, password = pwd, host=host, database=db) cursor = conn.cursor() data = conn.cursor() tables = [] try: cursor.execute(‘SHOW TABLES‘) for (table_name,) in cursor: tables.append(table_name) except mysql.connector.Error as err: print(‘Error:‘) print(err.msg) sys.exit() cursor.close(); #print(tables) for tn in tables: #data = conn.cursor() try: sql = ‘DROP TABLE `‘ + tn + ‘`;‘ data.execute(sql) print(sql) except mysql.connector.Error as err: print(‘Error:‘) print(err.msg) #sys.exit() #data.close() conn.commit() conn.close()
与数据连接时,涉及到数据的更新、删除、插入等操作,需要提交。