连接mysql数据库



我在macOS上使用Python 3.9版本的conda虚拟环境。

我正在尝试连接到mysql数据库,像这样:

import MySQLdb
# Connect
db = MySQLdb.connect(
host="jgsahkjrds.amazonaws.com",
user="admin",
password="Jabc!",
)
cursor = db.cursor()
# Execute SQL select statement
cursor.execute("SELECT * from z3")
# Commit your changes if writing
# In this case, we are only reading data
# db.commit()
# Get the number of rows in the resultset
numrows = cursor.rowcount
# Get and display one row at a time
for x in range(0, numrows):
row = cursor.fetchone()
print(row[0])
# Close the connection
db.close()

运行代码会产生如下错误:

ModuleNotFoundError没有名为'MySQLdb'的模块

当我尝试做pip install MySQLdb时,我得到这个:

无法找到满足要求的MySQLdb (from
ERROR: No matching distribution for MySQLdb

还有什么方法可以连接到我的数据库?

这个网站有你正在寻找的答案。你需要mysqlclient, mysql-connector-python和PyMySQL。