有一个名为Pymysql的模块,您可能会喜欢:
"""This pure Python MySQL client provides a DB-API to a MySQL database by talking directly to the server via the binary client/server protocol."""
import pymysql
conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql')
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
for response in cur:
print(response)
cur.close()
conn.close()
mysql
移植到Python 3上,但至少有两个纯Python模块可以在Python 3(以及PyPy等)上正常工作:
通过快速谷歌搜索 python3 mysql 可以找到另外几个模块(以及各种指向这两个项目的指针,包括之前提出同样问题的SO问题)。