连接到MySQL数据库Python3.9

发布于 2025-02-07 14:55:18 字数 813 浏览 2 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方。的韩爷 2025-02-14 14:55:18

this

This site has the answer you're looking for. You'll need mysqlclient, mysql-connector-python and PyMySQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文