用于 python 3.7 的 MySQL 包

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

MySQL package for python 3.7

mysqlpython-3.7

提问by NotanNimda

I am in the need of saving data into a MySQL database, my problem is I can't find the package...

我需要将数据保存到 MySQL 数据库中,我的问题是我找不到包...

Solution explored:

探索的解决方案:

So if anyone has an alternative to it for python 3.7, or knows how to get the connector for version 3.7 I would be happy.

因此,如果有人有 python 3.7 的替代方案,或者知道如何获取 3.7 版的连接器,我会很高兴。

回答by Freeman

mysqlclientsupports python3.7officially, you can find it here :

mysqlclientpython3.7官方支持,你可以在这里找到它:

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

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

1)you can download , PyMySQL 0.9.2
2)extract & copy the folder pymysqlinto the python Libfolder
3)and for connection you can do like this(make a file for example freeman.py):

1)您可以下载PyMySQL 0.9.2
2)将文件夹提取并复制pymysql到 pythonLib文件夹中
3)为了连接,您可以这样做(例如制作一个文件freeman.py):

#!/usr/bin/env python

import pymysql

conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='freemanDB')

cur = conn.cursor()
cur.execute("SELECT * FROM users")

print(cur.description)
print()

for row in cur:
    print(row)

cur.close()
conn.close()

回答by Ryan

There are two ways to install MySQL connector. The second way is preferred.

有两种安装 MySQL 连接器的方法。第二种方式是首选。

1. MySQL Installer

1. MySQL 安装程序

This is Oracle's product installer. The problem is that it is outdated. It is only aware of Python version 3.6, and nothing newer. To install for a newer version of Python, use option 2.

这是 Oracle 的产品安装程序。问题是它已经过时了。它只知道 Python 3.6 版,没有更新。要安装更新版本的 Python,请使用选项 2。

2. Python package manager, pip

2. Python 包管理器, pip

The Python package manager comes with Python, called pip. It downloads the package from the PyPIrepository and installs it in an automatic location based on what version of Python (or what virtual copy) you use to install it. The package that you want is mysql-connector-python. In fact, the official documentationsays this is the recommended method for installing the MySQL Connector.

Python 包管理器随 Python 一起提供,称为pip. 它从PyPI存储库下载包,并根据您用于安装它的 Python 版本(或虚拟副本)将其安装在自动位置。你想要的包是mysql-connector-python. 事实上,官方文档说这是安装 MySQL Connector 的推荐方法。

For example, on Windows, open up the Command Prompt (cmd.exe) by searching in the Start Menu. Navigate to the directory where pipis installed. Or make sure that pip's directory is included in the $PATH variable (which you can edit by searching for "PATH" in the Start Menu).

例如,在 Windows 上,cmd.exe通过在“开始”菜单中搜索来打开命令提示符 ( )。导航到pip安装目录。或者确保pip的目录包含在 $PATH 变量中(您可以通过在“开始”菜单中搜索“PATH”进行编辑)。

The command prompt will show you this:

命令提示符将显示以下内容:

PS C:\Users\Ryan> 

except instead of my username, it'll show your username, or the path you navigated to after you found pip, like "C:\blah\blah". Then use this command...

除了我的用户名之外,它会显示您的用户名,或您找到后导航到的路径pip,例如“C:\blah\blah”。然后用这个命令...

Input:

输入:

pip install mysql-connector-python

It downloads and installs it.

它会下载并安装它。

Results:

结果:

PS C:\Users\Ryan> pip install mysql-connector-python
Collecting mysql-connector-python
Downloading https://files.pythonhosted.org/packages/2d/65/3fc902c0f7635912800c6b935313b99b9d4426419ef7ba04f76231b24923/mysql_connector_python-8.0.12-py2.py3-none-any.whl (300kB)
    100% |████████████████████████████████| 307kB 1.1MB/s
Collecting protobuf>=3.0.0 (from mysql-connector-python)
Downloading https://files.pythonhosted.org/packages/77/78/a7f1ce761e2c738e209857175cd4f90a8562d1bde32868a8cd5290d58926/protobuf-3.6.1-py2.py3-none-any.whl (390kB)
    100% |████████████████████████████████| 399kB 1.8MB/s
Requirement already satisfied: setuptools in c:\users\ryan\appdata\local\programs\python\python37\lib\site-packages (from protobuf>=3.0.0->mysql-connector-python) (40.4.3)
Collecting six>=1.9 (from protobuf>=3.0.0->mysql-connector-python)
Using cached https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Installing collected packages: six, protobuf, mysql-connector-python
Successfully installed mysql-connector-python-8.0.12 protobuf-3.6.1 six-1.11.0

Afterward, you can make sure it is installed by displaying all packages that have been installed by you (not including standard libraries, which come with Python):

之后,您可以通过显示您已安装的所有包(不包括 Python 附带的标准库)来确保它已安装:

Input:

输入:

PS C:\Users\Ryan> pip list

Results:

结果:

Package                Version
---------------------- -------
mysql-connector-python 8.0.12
pip                    18.0
protobuf               3.6.1
setuptools             40.4.3
six                    1.11.0

To check if you installed it to the right version of Python, use pip -V. To make sure you installed it to the right copy (virtual or original), look at the file path where the package was installed:

要检查您是否将其安装到正确版本的 Python,请使用pip -V. 要确保将其安装到正确的副本(虚拟或原始),请查看安装包的文件路径:

PS C:\Users\Ryan> pip -V

Results:

结果:

pip 18.0 from c:\users\ryan\appdata\local\programs\python\python37\lib\site-packages\pip (python 3.7)

For example, in my system, it shows python37in the folder path, so it installed it to the original Python 3.7.0 installation I have, instead of an older version or virtual environment (condaor virtualenv, etc).

例如,在我的系统中,它显示python37在文件夹路径中,因此它将它安装到我拥有的原始 Python 3.7.0 安装中,而不是旧版本或虚拟环境(condavirtualenv等)。

To check the version using the Python executable, instead of pip:

要使用 Python 可执行文件检查版本,而不是pip

PS C:\Users\Ryan> py -V
Python 3.7.0

If you need to install it to an older or newer Python version than the default installation, insert the version number as an option (aka. "switch") in the command, using -. For example, to select version 3.6:

如果需要将其安装到比默认安装更旧或更新的 Python 版本,请在命令中插入版本号作为选项(又名“开关”),使用-. 例如,要选择版本 3.6:

py -3.6 -m pip install mysql-connector-python

The -works on both Windows and Unix-like OSs.

-Windows和Unix类操作系统的工作原理。

回答by Mohammad Rijwan

pip install mysql-connector-python

This command on cmd will solve the problem. Run cmd as administrator if in any case you face problem.

cmd 上的这个命令将解决这个问题。如果在任何情况下遇到问题,请以管理员身份运行 cmd。

回答by phuser

You must install flask_mysqldb package in order to use MySQL

你必须安装flask_mysqldb包才能使用MySQL

pip install flask_mysqldb

To use it:

要使用它:

from project import app

from flask_mysqldb import MySQL

Config MySQL:

配置 MySQL:

app.config['MYSQL_HOST'] = 'localhost'

app.config['MYSQL_USER'] = 'root'

app.config['MYSQL_PASSWORD'] = 'yourpassword'

app.config['MYSQL_DB'] = 'mydbfile'

app.config['MYSQL_CURSORCLASS'] = 'MyCursor'