Python Data Access 简明教程

Python MySQL - Introduction

Python 数据库接口的 Python 标准是 Python DB-API。大多数 Python 数据库接口都遵守此标准。

您可以为您的应用程序选择合适的数据库。Python Database API 支持范围广泛的数据库服务器,例如−

  1. GadFly

  2. mSQL

  3. MySQL

  4. PostgreSQL

  5. Microsoft SQL Server 2000

  6. Informix

  7. Interbase

  8. Oracle

  9. Sybase

以下是可用的 Python 数据库接口列表: Python Database Interfaces and APIs 。您必须为每个要访问的数据库下载一个单独的 DB API 模块。例如,如果您需要访问 Oracle 数据库和 MySQL 数据库,则必须下载 Oracle 和 MySQL 数据库模块。

What is mysql-connector-python?

MySQL Python/Connector 是一个接口,用于从 Python 连接到 MySQL 数据库服务器。它实现了 Python Database API,并建立在 MySQL 之上。

How do I Install mysql-connector-python?

首先,您需要确保已在计算机中安装 Python。要执行此操作,请打开命令提示符并在其中键入 python,然后按 Enter 键。如果您的系统中已安装 Python,此命令将显示其版本,如下所示−

C:\Users\Tutorialspoint>python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

现在按 ctrl+z,然后按 Enter 键退出 Python 外壳并创建一个名为 Python_MySQL 的文件夹(您打算在其中安装 Python-MySQL 连接器),如下所示−

>>> ^Z
C:\Users\Tutorialspoint>d:
D:\>mkdir Python_MySQL

Verify PIP

PIP 是 Python 中的一个包管理器,使用 PIP 可以安装 Python 中的各种模块/包。因此,若要安装 Mysql-Python mysql-connector-python,您需要确保已在计算机中安装 PIP,并将其位置添加到路径中。

您可以通过执行 pip 命令来执行此操作。如果系统中没有 PIP,或者如果尚未将其实例添加到 Path 环境变量中,则会收到错误消息,如下所示−

D:\Python_MySQL>pip
'pip' is not recognized as an internal or external command,
operable program or batch file.

若要安装 PIP,请下载把 get-pip.py 下载到以上创建的文件夹中,在命令中导航并按照以下步骤安装 pip−

D:\>cd Python_MySQL
D:\Python_MySQL>python get-pip.py
Collecting pip
Downloading https://files.pythonhosted.org/packages/8d/07/f7d7ced2f97ca3098c16565efbe6b15fafcba53e8d9bdb431e09140514b0/pip-19.2.2-py2.py3-none-any.whl (1.4MB)
|████████████████████████████████| 1.4MB 1.3MB/s
Collecting wheel
Downloading https://files.pythonhosted.org/packages/00/83/b4a77d044e78ad1a45610eb88f745be2fd2c6d658f9798a15e384b7d57c9/wheel-0.33.6-py2.py3-none-any.whl
Installing collected packages: pip, wheel
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-19.2.2 wheel-0.33.6

Installing mysql-connector-python

安装 Python 和 PIP 后,打开命令提示符并升级 pip(可选),如下所示−

C:\Users\Tutorialspoint>python -m pip install --upgrade pip
Collecting pip
Using cached https://files.pythonhosted.org/packages/8d/07/f7d7ced2f97ca3098c16565efbe6b15fafcba53e8d9bdb431e09140514b0/pip-19.2.2-py2.py3-none-any.whl
Python Data Access
4
Installing collected packages: pip
Found existing installation: pip 19.0.3
Uninstalling pip-19.0.3:
Successfully uninstalled pip-19.0.3
Successfully installed pip-19.2.2

然后以管理员模式打开命令提示符,并安装 Python MySQL connect,如下所示:

C:\WINDOWS\system32>pip install mysql-connector-python
Collecting mysql-connector-python
Using cached https://files.pythonhosted.org/packages/99/74/f41182e6b7aadc62b038b6939dce784b7f9ec4f89e2ae14f9ba8190dc9ab/mysql_connector_python-8.0.17-py2.py3-none-any.whl
Collecting protobuf>=3.0.0 (from mysql-connector-python)
Using cached https://files.pythonhosted.org/packages/09/0e/614766ea191e649216b87d331a4179338c623e08c0cca291bcf8638730ce/protobuf-3.9.1-cp37-cp37m-win32.whl
Collecting six>=1.9 (from protobuf>=3.0.0->mysql-connector-python)
Using cached https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Requirement already satisfied: setuptools in c:\program files (x86)\python37-32\lib\site-packages (from protobuf>=3.0.0->mysql-connector-python) (40.8.0)
Installing collected packages: six, protobuf, mysql-connector-python
Successfully installed mysql-connector-python-8.0.17 protobuf-3.9.1 six-1.12.0

Verification

若要验证安装,请创建包含以下行的示例 python 脚本。

import mysql.connector

如果安装成功,在执行该脚本后,不应出现任何错误:

D:\Python_MySQL>python test.py
D:\Python_MySQL>

Installing python from scratch

简单来说,如果你需要从头开始安装 Python。请访问 Python Home Page

installing python

点击 Downloads 按钮,你将被重定向到下载页面,该页面提供各种平台的最新版 Python 链接,选择一个并下载。

python downloads

例如,我们下载了 python-3.7.4.exe(适用于 Windows)。双击下载的 .exe 文件,开始安装过程。

start the installation

选中“将 Python 3.7 添加到路径”选项并继续安装。完成此过程后,python 将安装在你的系统中。

after installation