python2和python3 分别连接MySQL的代码

python2中的写法如下:

 1 #coding=utf-8
 2 import MySQLdb
 3 
 4 try:
 5     conn = MySQLdb.connect(host='localhost', port=3306, db='python3', user='root', passwd='mysql', charset='utf8')
 6     cursor1 = conn.cursor()
 7 
 8     sql = 'insert into students(name) values("郭小二")'
 9     cursor1.execute(sql)
10     conn.commit()
11 
12     cursor1.close()
13     conn.close()
14 except Exception, e:
15     print(e.message)

python3中的写法如下:

 1 import pymysql
 2 
 3 try:
 4     conn = pymysql.connect(host='localhost', port=3306, db='python3', user='root', passwd='mysql', charset='utf8')
 5     cursor1 = conn.cursor()
 6 
 7     sql = 'insert into students(name) values("郭小二")'
 8     cursor1.execute(sql)
 9     conn.commit()
10 
11     cursor1.close()
12     conn.close()
13 except Exception:
14     print(e.message)
文章写来不易,转载请标注。。。欢迎关注!
原文地址:https://www.cnblogs.com/si-lei/p/9234956.html