python3连接mysql

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()

与数据连接时,涉及到数据的更新、删除、插入等操作,需要提交。

python3连接mysql,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。