如何使用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位版本,我认为你会在下面的评论中得到不支持的轮子错误。
推荐文章
- 子进程Popen和调用之间的区别是什么(我如何使用它们)?
- 将python 'type'对象转换为字符串
- 在pytest测试中记录日志
- 为什么MYSQL的高LIMIT偏移量减慢查询?
- .join()方法到底做什么?
- MySQL选择列不为空的位置
- 哪个更快:多个单个INSERT还是一个多行INSERT?
- 我如何知道一个生成器从一开始就是空的?
- 搜索mysql数据库中出现的所有字符串
- Python中的SFTP ?(平台无关)
- 使用Python将XML转换为JSON ?
- 如何修改文本文件?
- 如何更改默认的Anaconda python环境
- 在pandas应用函数中获取一行的索引
- 为什么PyCharm提议将方法改为静态?