
- Python 資料訪問教程
- Python 資料訪問 - 首頁
- Python MySQL
- Python MySQL - 簡介
- Python MySQL - 資料庫連線
- Python MySQL - 建立資料庫
- Python MySQL - 建立表
- Python MySQL - 插入資料
- Python MySQL - 選擇資料
- Python MySQL - Where 子句
- Python MySQL - Order By
- Python MySQL - 更新表
- Python MySQL - 刪除資料
- Python MySQL - 刪除表
- Python MySQL - Limit
- Python MySQL - Join
- Python MySQL - 遊標物件
- Python PostgreSQL
- Python PostgreSQL - 簡介
- Python PostgreSQL - 資料庫連線
- Python PostgreSQL - 建立資料庫
- Python PostgreSQL - 建立表
- Python PostgreSQL - 插入資料
- Python PostgreSQL - 選擇資料
- Python PostgreSQL - Where 子句
- Python PostgreSQL - Order By
- Python PostgreSQL - 更新表
- Python PostgreSQL - 刪除資料
- Python PostgreSQL - 刪除表
- Python PostgreSQL - Limit
- Python PostgreSQL - Join
- Python PostgreSQL - 遊標物件
- Python SQLite
- Python SQLite - 簡介
- Python SQLite - 建立連線
- Python SQLite - 建立表
- Python SQLite - 插入資料
- Python SQLite - 選擇資料
- Python SQLite - Where 子句
- Python SQLite - Order By
- Python SQLite - 更新表
- Python SQLite - 刪除資料
- Python SQLite - 刪除表
- Python SQLite - Limit
- Python SQLite - Join
- Python SQLite - 遊標物件
- Python MongoDB
- Python MongoDB - 簡介
- Python MongoDB - 建立資料庫
- Python MongoDB - 建立集合
- Python MongoDB - 插入文件
- Python MongoDB - 查詢
- Python MongoDB - 查詢
- Python MongoDB - 排序
- Python MongoDB - 刪除文件
- Python MongoDB - 刪除集合
- Python MongoDB - 更新
- Python MongoDB - Limit
- Python 資料訪問資源
- Python 資料訪問 - 快速指南
- Python 資料訪問 - 有用資源
- Python 資料訪問 - 討論
Python MySQL - 簡介
Python 資料庫介面的標準是 Python DB-API。大多數 Python 資料庫介面都遵循此標準。
您可以為您的應用程式選擇合適的資料庫。Python 資料庫 API 支援各種資料庫伺服器,例如:
- GadFly
- mSQL
- MySQL
- PostgreSQL
- Microsoft SQL Server 2000
- Informix
- Interbase
- Oracle
- Sybase
以下是可用的 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 並按 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 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 的連結,選擇一個並下載。

例如,我們下載了 python-3.7.4.exe(適用於 Windows)。透過雙擊下載的.exe檔案開始安裝過程。

選中“將 Python 3.7 新增到 PATH”選項並繼續安裝。完成此過程後,python 將安裝到您的系統中。
