python-连接MySQL(mysql.connector与MySQLdb区别)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaobao5214/article/details/82712778

 

1.安装mysql-connector 命令    pip install mysql-connector 

import mysql.connector
cnx = mysql.connector.connect(user='root', password='123456',host='127.0.0.1',database='employees')
cnx.close()

2.安装 MySQLdb命令   pip install  MySQLdb

(该适用linux不适用windows, 在windows环境中需要安装exe下载地址 http://www.codegood.com/downloads)

import MySQLdb

db = MySQLdb.connect(host="localhost", # your host, usually localhost
                     user="root", # your username
                      passwd="123456", # your password
                      db="employeedb",
                      port = 3306) # name of the data base

cur = db.cursor() 
cur.execute("select * from emp")
 

两者区别:

  mysql-connector是一个Python模块,它在Python中重新实现MySQL协议,它比较慢,但不需要C库,因此更便携。

MySqLDB是一个C模块,它使用MySQL客户端库中的MySQL协议实现相链接,它更快,但是需要C库才能工作。


猜你喜欢

转载自blog.csdn.net/xiaobao5214/article/details/82712778