连接到MySQL数据库Python3.9
我正在使用MacOS上的Python版本3.9使用Conda虚拟环境。
我试图将其连接到MySQL DB:
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 no模块名为“ mysqldb”
时
找不到满足要求mysqldb的版本(来自 版本:无)
错误:MySQLDB找不到匹配分布
我可以将其他哪些连接到DB连接?
I am using a conda virtual environment with Python version 3.9 on macOS.
I am trying to connect to a mysql db like this:
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()
Running the code gives me this error:
ModuleNotFoundError No module named 'MySQLdb'
When I try to do pip install MySQLdb
, I get this:
Could not find a version that satisfies the requirement MySQLdb (from
versions: none)
ERROR: No matching distribution found for MySQLdb
What are other ways I can connect to my db?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
this
This site has the answer you're looking for. You'll need mysqlclient, mysql-connector-python and PyMySQL.