
- 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 - 資料選擇
您可以使用 SELECT 查詢從 MySQL 中的表中檢索/提取資料。此查詢/語句以表格形式返回指定表的內容,稱為結果集。
語法
以下是 SELECT 查詢的語法:
SELECT column1, column2, columnN FROM table_name;
示例
假設我們在 MySQL 中建立了一個名為 cricketers_data 的表,如下所示:
CREATE TABLE cricketers_data( First_Name VARCHAR(255), Last_Name VARCHAR(255), Date_Of_Birth date, Place_Of_Birth VARCHAR(255), Country VARCHAR(255) );
如果我們使用 INSERT 語句向其中插入了 5 條記錄,如下所示:
insert into cricketers_data values( 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India'); insert into cricketers_data values( 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica'); insert into cricketers_data values( 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka'); insert into cricketers_data values( 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India'); insert into cricketers_data values( 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India');
以下查詢從表中檢索 FIRST_NAME 和 Country 值。
mysql> select FIRST_NAME, Country from cricketers_data; +------------+-------------+ | FIRST_NAME | Country | +------------+-------------+ | Shikhar | India | | Jonathan | SouthAfrica | | Kumara | Srilanka | | Virat | India | | Rohit | India | +------------+-------------+ 5 rows in set (0.00 sec)
您還可以使用 * 代替列名來檢索每條記錄的所有值,如下所示:
mysql> SELECT * from cricketers_data; +------------+------------+---------------+----------------+-------------+ | First_Name | Last_Name | Date_Of_Birth | Place_Of_Birth | Country | +------------+------------+---------------+----------------+-------------+ | Shikhar | Dhawan | 1981-12-05 | Delhi | India | | Jonathan | Trott | 1981-04-22 | CapeTown | SouthAfrica | | Kumara | Sangakkara | 1977-10-27 | Matale | Srilanka | | Virat | Kohli | 1988-11-05 | Delhi | India | | Rohit | Sharma | 1987-04-30 | Nagpur | India | +------------+------------+---------------+----------------+-------------+ 5 rows in set (0.00 sec)
使用 Python 讀取 MYSQL 表中的資料
任何資料庫的讀取操作都意味著從資料庫中提取一些有用的資訊。您可以使用 mysql-connector-python 提供的 `fetch()` 方法從 MYSQL 中提取資料。
`cursor.MySQLCursor` 類提供了三種方法,即 `fetchall()`、`fetchmany()` 和 `fetchone()`,其中:
`fetchall()` 方法檢索查詢結果集中的所有行,並將其作為元組列表返回。(如果我們在檢索幾行後執行此操作,它將返回其餘行)。
`fetchone()` 方法檢索查詢結果中的下一行,並將其作為元組返回。
`fetchmany()` 方法類似於 `fetchone()`,但它檢索查詢結果集中的下一組行,而不是單行。
**注意** - 結果集是在使用遊標物件查詢表時返回的物件。
**rowcount** - 這是一個只讀屬性,返回受 `execute()` 方法影響的行數。
示例
以下示例使用 SELECT 查詢提取 EMPLOYEE 表的所有行,並從獲得的結果集中,我們首先使用 `fetchone()` 方法檢索第一行,然後使用 `fetchall()` 方法檢索其餘行。
import mysql.connector #establishing the connection conn = mysql.connector.connect( user='root', password='password', host='127.0.0.1', database='mydb') #Creating a cursor object using the cursor() method cursor = conn.cursor() #Retrieving single row sql = '''SELECT * from EMPLOYEE''' #Executing the query cursor.execute(sql) #Fetching 1st row from the table result = cursor.fetchone(); print(result) #Fetching 1st row from the table result = cursor.fetchall(); print(result) #Closing the connection conn.close()
輸出
('Krishna', 'Sharma', 19, 'M', 2000.0) [('Raj', 'Kandukuri', 20, 'M', 7000.0), ('Ramya', 'Ramapriya', 25, 'M', 5000.0)]
以下示例使用 `fetchmany()` 方法檢索 EMPLOYEE 表的前兩行。
示例
import mysql.connector #establishing the connection conn = mysql.connector.connect( user='root', password='password', host='127.0.0.1', database='mydb') #Creating a cursor object using the cursor() method cursor = conn.cursor() #Retrieving single row sql = '''SELECT * from EMPLOYEE''' #Executing the query cursor.execute(sql) #Fetching 1st row from the table result = cursor.fetchmany(size =2); print(result) #Closing the connection conn.close()
輸出
[('Krishna', 'Sharma', 19, 'M', 2000.0), ('Raj', 'Kandukuri', 20, 'M', 7000.0)]