如何使用pip安装Python的MySQLdb模块?


当前回答

如果pip3不起作用,您可以尝试:

sudo apt install python3-mysqldb

其他回答

转到pycharm,然后转到默认设置——> pip(双击)——pymsqldb..——> install——在像这样的程序中安装使用之后

import pymysql as MySQLdb

# Open database connection
db = MySQLdb.connect("localhost","root","root","test" )

# prepare a cursor object using cursor() method
cursor = db.cursor()

# execute SQL query using execute() method.
cursor.execute("show tables")

# Fetch a single row using fetchone() method.
data = cursor.fetchall()
print (data)

# disconnect from server
db.close()

你可以去这个网站下载软件包。

我的环境是:

Windows 10 Pro, Python 3.7 (Python -3.7.1-amd64.exe) MySQL 8.0 (MySQL -install -web-community-8.0.13.0 msi)

PIP安装mysqlclient-1.3.13-cp37-cp37m-win_amd64.whl

对我有用。

import MySQLdb, sys


# --------------------------------------------------
# Connect to MySQL
# --------------------------------------------------
try:
    db = MySQLdb.connect(host="localhost", user="user", passwd="pass", db="database", charset='cp1251')
except MySQLdb.Error as e:
    print ("Error %d: %s" % (e.args[0], e.args[1]))
    sys.exit()

# Creating cursor 
cursor = db.cursor()

如果你的系统上安装了Windows,那么在cmd上输入以下命令:

pip install mysql-connector

如果上述命令不起作用,请尝试使用:

pip install mysql-connector-python

现在,如果上面的命令不能完成工作,尝试使用:

pip install mysql-connector-python-rf

好了,你现在可以走了。

我在通过Pip(问题编译源)在Windows上安装64位版本的MySQLdb时遇到了问题[32位版本安装ok]。设法从http://www.lfd.uci.edu/~gohlke/pythonlibs/上的.whl文件安装编译后的MySQLdb

然后,.whl文件可以通过pip作为https://pip.pypa.io/en/latest/user_guide/#installing-from-wheels中的文档安装

例如,如果你保存在C:/ the,你可以安装通过

pip install c:/MySQL_python-1.2.5-cp27-none-win_amd64.whl

后续:如果你安装了64位版本的Python,那么你想从上面的链接安装64位AMD版本的MySQLdb[即,即使你有英特尔处理器]。如果你尝试安装32位版本,我认为你会在下面的评论中得到不支持的轮子错误。