Python MySQL 简介

Python MySQL 简介

Python数据库接口的标准是Python DB-API。大多数Python数据库接口都遵从这个标准。

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

这里是可用的Python数据库接口的列表:Python数据库接口和API。 你必须为你需要访问的每个数据库下载一个单独的 DB API 模块。例如,如果你需要访问一个Oracle数据库以及一个MySQL数据库,你必须同时下载Oracle和MySQL数据库模块。

什么是mysql-connector-python

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

如何安装mysql-connector-python

首先,你需要确保你的机器已经安装了python。为此,打开命令提示符,在其中输入python并按回车。如果系统中已经安装了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 ,然后按回车键,离开python shell,并创建一个文件夹(你打算在其中安装Python-MySQL连接器),命名为Python_MySQL,如-

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

验证PIP

PIP是Python中的一个包管理器,使用它你可以在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

安装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连接为—-。

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

验证

为了验证安装的情况,创建一个样本的python脚本,其中有以下一行。

import mysql.connector

如果安装成功,当你执行时,你不应该得到任何错误-

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

从头开始安装Python

很简单,如果你需要从头开始安装Python。请访问Python主页。

Python MySQL - 简介

点击 ** 下载** 按钮,你将被转到下载页面,该页面提供了不同平台的最新版本的Python的链接,选择一个并下载它。

Python MySQL - 简介

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

Python MySQL - 简介

勾选 Add Python 3.7 to Path 选项并继续安装。完成这个过程后,Python将被安装在你的系统中。

Python MySQL - 简介

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程