Python 3 和 mysql 通过 SQLAlchemy
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14164183/
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
Python 3 and mysql through SQLAlchemy
提问by martinqt
Currently:
目前:
- SQLAlchemy installed and working (or at least import v0.8.0b2)
- Mysql (v5.5.16)
- Distribute (0.6.34)
- Oracle mysql-python connector
- Python 3.2
- Windows 7 32/64 (note that I installed Python 32bits)
- 安装并运行 SQLAlchemy(或至少导入 v0.8.0b2)
- mysql (v5.5.16)
- 分发 (0.6.34)
- Oracle mysql-python 连接器
- 蟒蛇 3.2
- Windows 7 32/64(注意我安装了 Python 32bits)
The problem is that MySQLdb or Oursql is required and I didn't managed to get any of them working.
问题是 MySQLdb 或 Oursql 是必需的,我没有设法让它们中的任何一个工作。
Found thisbut didn't manage to get it working neither.
找到了这个,但也没有设法让它工作。
Edit: If you are aware of an other orm that works with Python3, I'm interested.
编辑:如果您知道其他适用于 Python3 的 orm,我很感兴趣。
回答by Brad Campbell
I was successful in getting Oracle's MySQL connector for python working with SQLAlchemy on Python 3.3. Your connection string needs to start with "mysql+mysqlconnector://...". After I changed my connection string everything (well, simple things) started working.
我成功地让 Oracle 的 MySQL 连接器与 Python 3.3 上的 SQLAlchemy 一起使用。您的连接字符串需要以“mysql+mysqlconnector://...”开头。在我更改了连接字符串之后,一切(好吧,简单的事情)都开始工作了。
The MySQL connector docs can be found here: https://dev.mysql.com/doc/connector-python/en/
MySQL 连接器文档可以在这里找到:https: //dev.mysql.com/doc/connector-python/en/
The package is up on PyPi: https://pypi.org/project/mysql-connector-python/
该包在 PyPi 上:https://pypi.org/project/mysql-connector-python/
Here are the SQLAlchemy docs about using the Python connector: http://docs.sqlalchemy.org/en/latest/dialects/mysql.html#module-sqlalchemy.dialects.mysql.mysqlconnector
以下是有关使用 Python 连接器的 SQLAlchemy 文档:http: //docs.sqlalchemy.org/en/latest/dialects/mysql.html#module-sqlalchemy.dialects.mysql.mysqlconnector
回答by Fletch F Fletch
For others who arrive here, this should do it:
对于到达这里的其他人,应该这样做:
- pip install mysql-connector==2.1.4 # version avoids Protobuf error
- URI = 'mysql+mysqlconnector://$USER:$PASS@$HOST/$DB'
- pip install mysql-connector==2.1.4 # 版本避免 Protobuf 错误
- URI = 'mysql+mysqlconnector://$USER:$PASS@$HOST/$DB'
回答by seaders
I tried Oracle's connection, as suggested by @Brad Campbell, but unfortunately it was extremelyslow, much slower than the "real" MySQL-Python
connection I had been using with SQLAlchemy
on Python 2.
我按照@Brad Campbell 的建议尝试了 Oracle 的连接,但不幸的是它非常慢,比MySQL-Python
我SQLAlchemy
在 Python 2 上使用的“真实”连接慢得多。
After checking SQLAlchemy themselves,
在自己检查了 SQLAlchemy 之后,
http://docs.sqlalchemy.org/en/latest/dialects/mysql.html#module-sqlalchemy.dialects.mysql.mysqldb
http://docs.sqlalchemy.org/en/latest/dialects/mysql.html#module-sqlalchemy.dialects.mysql.mysqldb
To use MySQL-Python
on Python 3, they recommend a fork of it, mysqlclient
,
要MySQL-Python
在 Python 3 上使用,他们推荐它的一个分支mysqlclient
,
https://github.com/PyMySQL/mysqlclient-python
https://github.com/PyMySQL/mysqlclient-python
It is available via pip with pip install mysqlclient
, but there are almost certainly other steps you'll need to do to set it up initially. After that though, I was seeing the performance go back to what I was used to, which was about 5x faster than with Oracle's connector.
它可以通过 pip with 获得pip install mysqlclient
,但几乎可以肯定,您需要执行其他步骤来最初设置它。在那之后,我看到性能恢复到我习惯的状态,比使用 Oracle 的连接器快 5 倍左右。
回答by mrgrieves
I've gotten oursql + SQLAlchemy 0.8.1 + Python 3.3 to work. Building off of LukeCarrier's port, I modified oursql.c to use the correct import levels, and it worked! Try this, and be sure to follow the readme:
我已经让 oursql + SQLAlchemy 0.8.1 + Python 3.3 工作了。基于 LukeCarrier 的端口构建,我修改了 oursql.c 以使用正确的导入级别,并且成功了!试试这个,并确保遵循自述文件:
https://github.com/clintron/py3k-oursql
https://github.com/clintron/py3k-oursql
You may also need to have the latest version of Cython.
您可能还需要拥有最新版本的 Cython。