带有 MySQL 数据库的 Python 3.4.0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23376103/
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-19 02:51:46  来源:igfitidea点击:

Python 3.4.0 with MySQL database

pythonmysqlpython-3.xmysql-pythonpython-3.4

提问by Srdan Ristic

I have installed Python version3.4.0and I would like to do a project with MySQL database. I downloaded and tried installing MySQLdb, but it wasn't successful for this version of Python. Any suggestions how could I fix this problem and install it properly?

我已经安装了Python 3.4.0,我想用 MySQL 数据库做一个项目。我下载并尝试安装MySQLdb,但对于这个版本的 Python 没有成功。任何建议如何解决此问题并正确安装?

采纳答案by Andrew Gorcester

MySQLdb does not support Python 3 but it is not the only MySQL driver for Python.

MySQLdb 不支持 Python 3,但它不是 Python 的唯一 MySQL 驱动程序。

mysqlclientis essentially just a fork of MySQLdb with Python 3 support merged in (and a few other improvements).

mysqlclient本质上只是 MySQLdb 的一个分支,合并了 Python 3 支持(以及其他一些改进)。

PyMySQLis a pure python MySQL driver, which means it is slower, but it does not require a compiled C component or MySQL libraries and header files to be installed on client machines. It has Python 3 support.

PyMySQL是一个纯 python MySQL 驱动程序,这意味着它更慢,但它不需要在客户端机器上安装编译的 C 组件或 MySQL 库和头文件。它具有 Python 3 支持。

Another option is simply to use another database system like PostgreSQL.

另一种选择是简单地使用另一个数据库系统,如PostgreSQL

回答by lxx

Use mysql-connector-python. I prefer to install it with pip from PyPI:

使用mysql-connector-python。我更喜欢用PyPI 的pip 安装它:

pip install --allow-external mysql-connector-python mysql-connector-python

Have a look at its documentationand examples.

查看它的文档示例

If you are going to use pooling make sure your database has enough connections available as the default settings may not be enough.

如果您打算使用池化,请确保您的数据库有足够的可用连接,因为默认设置可能不够。

回答by AbsoluteMSTR

mysqlclientis a fork of MySQLdb and can serve as a drop-in replacement with Python 3.4 support. If you have trouble building it on Windows, you can download it from Christoph Gohlke's Unofficial Windows Binaries for Python Extension Packages

mysqlclient是 MySQLdb 的一个分支,可以作为 Python 3.4 支持的替代品。如果在 Windows 上构建时遇到问题,可以从Christoph Gohlke 的非官方 Windows 二进制文件中下载,用于 Python 扩展包

回答by Steve R.

There is a Ubuntu solution available either through the Ubuntu Software Center or through the Synaptic Package Manager. This will connect Python version 3.4.0 to MySQL. Download "python3-mysql.connector" version 1.1.6-1.

通过 Ubuntu 软件中心或 Synaptic 软件包管理器可以使用 Ubuntu 解决方案。这会将 Python 3.4.0 版连接到 MySQL。下载“ python3-mysql.connector”版本1.1.6-1。

Note that the connection syntax does notuse "MySQLdb". Instead read: Connecting to MySQL Using Connector/Python

请注意,连接语法并没有使用“ MySQLdb的”。而是阅读:使用连接器/Python 连接到 MySQL

回答by Frozen Flame

Alternatively, you can use mysqlclientor oursql. For oursql, use the oursql py3k seriesas my link points to.

或者,您可以使用mysqlclientoursql。对于oursql,使用oursql py3k 系列作为我的链接指向。

回答by Eduardo Basílio

Install pip:

安装点:

apt-get install pip

For acess MySQL from Python, install:

要从 Python 访问 MySQL,请安装:

pip3 install mysqlclient

回答by user1656671

It seems that at the moment Ubuntu 15.10 has a but with python3 and pip3.

目前 Ubuntu 15.10 似乎有一个但带有 python3 和 pip3。

As elaborated in this article.

本文所述

The problem makes pip3 install to python3.5 while python3 is actually running python3.4 :(

该问题使 pip3 安装到 python3.5,而 python3 实际上正在运行 python3.4 :(

Until a proper solution will be available via the updates you can do one of the following:

在通过更新提供合适的解决方案之前,您可以执行以下操作之一:

run

python3 -m pip install pymysql

instead of

代替

pip3 install pymysql

(or any other package)

(或任何其他包)

Now

现在

import pymysql

should work in python3 and in idle3.

应该在 python3 和 idle3 中工作。

Alternatively, if you explicitly need 3.5 you can use explicit python3.5 instead of python3. but idle3 will still point to 3.4...

或者,如果您明确需要 3.5,您可以使用显式 python3.5 而不是 python3。但 idle3 仍将指向 3.4 ...

回答by SanD

I solved it this way: download the zipped package from hereand follow this set of instructions:

我是这样解决的:从这里下载压缩包并按照以下说明操作:

unzip  /path/to/downloads/folder/mysql-connector-python-VER.zip  

In case u got a .gzu can use ->

如果你有一个.gz你可以使用 ->

tar xzf mysql-connector-python-VER.tar.gz 

And then:

进而:

cd mysql-connector-python-VER  # move into the directory

sudo python3 setup.py install # NOTICE I USED PYTHON3 INSTEAD OF PYTHON

You can read about it here

你可以在这里阅读

回答by imonaboat

Maybe you can use a work around and try something like:

也许您可以使用解决方法并尝试以下操作:

import datetime
#import mysql
import MySQLdb
conn = MySQLdb.connect(host = '127.0.0.1',user = 'someUser', passwd = 'foobar',db = 'foobardb')
cursor = conn.cursor()

回答by MinasA1

sudo apt-get install python3-dev
sudo apt-get install libmysqlclient-dev
sudo apt-get install zlib1g-dev
sudo pip3 install mysqlclient

that worked for me!

这对我有用!