如何在 Windows 上的 Python 3 中连接到 MySQL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4960048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 18:20:29  来源:igfitidea点击:

How can I connect to MySQL in Python 3 on Windows?

pythonmysqlpython-3.x

提问by panofish

I am using ActiveState Python 3 on Windows and wanted to connect to my MySQL database. I heard that mysqldbwas the module to use. I can't find mysqldbfor Python 3.

我在 Windows 上使用 ActiveState Python 3 并想连接到我的 MySQL 数据库。我听说这mysqldb是要使用的模块。我找不到mysqldbPython 3。

Is there a repository available where the binaries exist for mysqldb? How can I connect to MySQL in Python 3 on Windows?

是否有可用的存储库,其中存在二进制文件mysqldb?如何在 Windows 上的 Python 3 中连接到 MySQL?

采纳答案by Collin Anderson

There are currently a few options for using Python 3 with mysql:

目前有几个选项可以将 Python 3 与 mysql 一起使用:

https://pypi.python.org/pypi/mysql-connector-python

https://pypi.python.org/pypi/mysql-connector-python

  • Officially supported by Oracle
  • Pure python
  • A little slow
  • Not compatible with MySQLdb
  • Oracle 官方支持
  • 纯蟒蛇
  • 有点慢
  • 与 MySQLdb 不兼容

https://pypi.python.org/pypi/pymysql

https://pypi.python.org/pypi/pymysql

  • Pure python
  • Faster than mysql-connector
  • Almost completely compatible with MySQLdb, after calling pymysql.install_as_MySQLdb()
  • 纯蟒蛇
  • 比 mysql-connector 快
  • MySQLdb调用后几乎完全兼容pymysql.install_as_MySQLdb()

https://pypi.python.org/pypi/cymysql

https://pypi.python.org/pypi/cymysql

  • fork of pymysql with optional C speedups
  • pymysql 的分支,带有可选的 C 加速

https://pypi.python.org/pypi/mysqlclient

https://pypi.python.org/pypi/mysqlclient

  • Django's recommended library.
  • Friendly fork of the original MySQLdb, hopes to merge back some day
  • The fastest implementation, as it is C based.
  • The most compatible with MySQLdb, as it is a fork
  • Debian and Ubuntu use it to provide both python-mysqldbandpython3-mysqldbpackages.
  • Django 的推荐库。
  • 原 MySQLdb 的友好分叉,希望有一天能合并回来
  • 最快的实现,因为它是基于 C 的。
  • 与 MySQLdb 最兼容,因为它是一个 fork
  • Debian 和 Ubuntu 使用它来提供python-mysqldbpython3-mysqldb包。

benchmarks here: https://github.com/methane/mysql-driver-benchmarks

这里的基准:https: //github.com/methane/mysql-driver-benchmarks

回答by casevh

Untested, but there are some binaries available at:

未经测试,但有一些二进制文件可用:

Unofficial Windows Binaries

非官方 Windows 二进制文件

回答by Oleh Prypin

You should probably use pymysql- Pure Python MySQL clientinstead.
It works with Python 3.x, and doesn't have any dependencies.

您可能应该改用pymysql- Pure Python MySQL 客户端
它适用于 Python 3.x,并且没有任何依赖项。

This pure Python MySQL client provides a DB-API to a MySQL database by talking directly to the server via the binary client/server protocol.

Example:

import pymysql
conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql')
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
for r in cur:
    print(r)
cur.close()
conn.close()

这个纯 Python MySQL 客户端通过二进制客户端/服务器协议直接与服务器通信,为 MySQL 数据库提供 DB-API。

例子:

import pymysql
conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql')
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
for r in cur:
    print(r)
cur.close()
conn.close()

回答by Bogd

I also tried using pymysql (on my Win7 x64 machine, Python 3.3), without too much luck. I downloaded the .tar.gz, extract, ran "setup.py install", and everything seemed fine. Until I tried connecting to a database, and got "KeyError [56]". An error which I was unable to find documented anywhere.

我也尝试过使用 pymysql(在我的 Win7 x64 机器上,Python 3.3),但运气不佳。我下载了 .tar.gz,解压,运行“setup.py install”,一切看起来都很好。直到我尝试连接到数据库,并得到“KeyError [56]”。我无法在任何地方找到记录的错误。

So I gave up on pymysql, and I settled on the Oracle MySQL connector.

所以我放弃了 pymysql,我选择了Oracle MySQL 连接器

It comes as a setup package, and works out of the box. And it also seems decently documented.

它是一个安装包,开箱即用。而且它似乎也有很好的记录。

回答by panofish

This does not fully answer my original question, but I think it is important to let everyone know what I did and why.

这并不能完全回答我最初的问题,但我认为让每个人都知道我做了什么以及为什么这样做很重要。

I chose to continue using python 2.7 instead of python 3 because of the prevalence of 2.7 examples and modules on the web in general.

我选择继续使用 python 2.7 而不是 python 3,因为 2.7 示例和模块在网络上普遍存在。

I now use both mysqldb and mysql.connector to connect to MySQL in Python 2.7. Both are great and work well. I think mysql.connector is ultimately better long term however.

我现在使用 mysqldb 和 mysql.connector 在 Python 2.7 中连接到 MySQL。两者都很棒,而且效果很好。然而,我认为 mysql.connector 从长远来看最终会更好。

回答by Lazik

CyMySQL https://github.com/nakagami/CyMySQL

CyMySQL https://github.com/nakagami/CyMySQL

I have installed pip on my windows 7, with python 3.3 just pip install cymysql

我已经在 Windows 7 上安装了 pip,使用 python 3.3 只需 pip install cymysql

(you don't need cython) quick and painless

(你不需要cython)快速无痛

回答by sagarchalise

PyMySQL gives MySQLDb like interface as well. You could try in your initialization:

PyMySQL 也提供类似 MySQLDb 的接口。您可以尝试初始化:

import pymysql
pymysql.install_as_MySQLdb()

Also there is a port of mysql-python on github for python3.

在 github 上还有一个用于 python3 的 mysql-python 端口。

https://github.com/davispuh/MySQL-for-Python-3

https://github.com/davispuh/MySQL-for-Python-3

回答by codeape

Oracle/MySQL provides an official, pure Python DBAPI driver: http://dev.mysql.com/downloads/connector/python/

Oracle/MySQL 提供了官方的纯 Python DBAPI 驱动程序:http://dev.mysql.com/downloads/connector/python/

I have used it with Python 3.3 and found it to work great. Also works with SQLAlchemy.

我已经将它与 Python 3.3 一起使用,并发现它工作得很好。也适用于 SQLAlchemy。

See also this question: Is it still too early to hop aboard the Python 3 train?

另见这个问题:现在跳上 Python 3 火车还为时过早吗?

回答by duke2909

On my mac os maverick i try this:

在我的 mac os 特立独行上,我试试这个:

After that, enter in the python3 interpreter and type:

之后,输入python3解释器并输入:

  1. import pymysql. If there is no error your installation is ok. For verification write a script to connect to mysql with this form:

  2. # a simple script for MySQL connection import pymysql db = pymysql.connect(host="localhost", user="root", passwd="*", db="biblioteca") #Sure, this is information for my db # close the connection db.close ()*

  1. 导入 pymysql。如果没有错误,您的安装就可以了。为了验证,编写一个脚本以使用以下形式连接到 mysql:

  2. # MySQL 连接的简单脚本 import pymysql db = pymysql.connect(host="localhost", user="root", passwd="*", db="biblioteca") #当然,这是我的数据库的信息 # 关闭连接 db.close ()*

Give it a name ("con.py" for example) and save it on desktop. In Terminal type "cd desktop" and then $python con.py If there is no error, you are connected with MySQL server. Good luck!

为其命名(例如“con.py”)并将其保存在桌面上。在终端输入“cd desktop”,然后输入$python con.py 如果没有错误,你就连接上了MySQL服务器。祝你好运!

回答by Jaco

I'm using cymysql with python3 on a raspberry pi I simply installed by: sudo pip3 install cython sudo pip3 install cymysql where cython is not necessary but should make cymysql faster

我在树莓派上使用带有 python3 的 cymysql 我只是通过以下方式安装: sudo pip3 install cython sudo pip3 install cymysql 其中 cython 不是必需的,但应该使 cymysql 更快

So far it works like a charm and very similar to MySQLdb

到目前为止,它就像一个魅力,与 MySQLdb 非常相似