首页 > 数据库技术 > 详细

python3连接mysql

时间:2014-03-23 19:41:56      阅读:483      评论:0      收藏:0      [点我收藏+]
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

原文:http://hnuxbl.blog.51cto.com/5029269/1381798

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!