程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Python >> Python連接Mysql常用的庫(mysqlclient示例)

Python連接Mysql常用的庫(mysqlclient示例)

編輯:Python

Python連接Mysql常用的庫有MySQLdb,mysqlclient,MySQL Connector/Python,但是django文檔上推薦使用mysqlclient,以下截取django官網關於MySQL API的介紹。

 

MySQL DB API Drivers

The Python Database API is described in PEP 249. MySQL has three prominent drivers that implement this API:
•MySQLdb is a native driver that has been developed and supported for over a decade by Andy Dustman.
•mysqlclient is a fork of MySQLdb which notably supports Python 3 and can be used as a drop-in replacement for MySQLdb. At the time of this writing, this is the recommended choice for using MySQL with Django.
•MySQL Connector/Python is a pure Python driver from Oracle that does not require the MySQL client library or any Python modules outside the standard library.

All these drivers are thread-safe and provide connection pooling. MySQLdb is the only one not supporting Python 3 currently.

In addition to a DB API driver, Django needs an adapter to access the database drivers from its ORM. Django provides an adapter for MySQLdb/mysqlclient while MySQL Connector/Python includes its own.

mysqlclient連接MySQL讀取數據示例:

import MySQLdb

db = MySQLdb.connect(host="localhost", user="root", passwd="", db="demo")
c = db.cursor()
max_price = 5
c.execute("""SELECT * FROM think_node""")
r = c.fetchone()
print(r)

 

首先通過pip工具安裝mysqlclient庫,命令:pip install mysqlclient

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved