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,布布扣,bubuko.com

python3连接mysql

上一篇:JDBC ResultSet获取Date类型相关


下一篇:被Oracle雷到了