
- 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 SQLite - 選擇資料
您可以使用 SELECT 查詢從 SQLite 表中檢索資料。此查詢/語句以表格形式返回指定關係(表)的內容,稱為結果集。
語法
以下是 SQLite 中 SELECT 語句的語法:
SELECT column1, column2, columnN FROM table_name;
示例
假設我們使用以下查詢建立了一個名為 CRICKETERS 的表:
sqlite> CREATE TABLE CRICKETERS ( First_Name VARCHAR(255), Last_Name VARCHAR(255), Age int, Place_Of_Birth VARCHAR(255), Country VARCHAR(255) ); sqlite>
如果我們使用 INSERT 語句向其中插入了 5 條記錄,如下所示:
sqlite> insert into CRICKETERS values('Shikhar', 'Dhawan', 33, 'Delhi', 'India'); sqlite> insert into CRICKETERS values('Jonathan', 'Trott', 38, 'CapeTown', 'SouthAfrica'); sqlite> insert into CRICKETERS values('Kumara', 'Sangakkara', 41, 'Matale', 'Srilanka'); sqlite> insert into CRICKETERS values('Virat', 'Kohli', 30, 'Delhi', 'India'); sqlite> insert into CRICKETERS values('Rohit', 'Sharma', 32, 'Nagpur', 'India'); sqlite>
以下 SELECT 查詢從 CRICKETERS 表中檢索列 FIRST_NAME、LAST_NAME 和 COUNTRY 的值。
sqlite> SELECT FIRST_NAME, LAST_NAME, COUNTRY FROM CRICKETERS; Shikhar |Dhawan |India Jonathan |Trott |SouthAfrica Kumara |Sangakkara |Srilanka Virat |Kohli |India Rohit |Sharma |India sqlite>
正如您所看到的,SQLite 資料庫的 SELECT 語句只是返回指定表的記錄。要獲得格式化的輸出,您需要在 SELECT 語句之前使用相應的命令設定header 和mode,如下所示:
sqlite> .header on sqlite> .mode column sqlite> SELECT FIRST_NAME, LAST_NAME, COUNTRY FROM CRICKETERS; First_Name Last_Name Country ---------- -------------------- ---------- Shikhar Dhawan India Jonathan Trott SouthAfric Kumara Sangakkara Srilanka Virat Kohli India Rohit Sharma India sqlite>
如果要檢索每條記錄的所有列,則需要用“*”替換列的名稱,如下所示:
sqlite> .header on sqlite> .mode column sqlite> SELECT * FROM CRICKETERS; First_Name Last_Name Age Place_Of_Birth Country ---------- ---------- ---------- -------------- ---------- Shikhar Dhawan 33 Delhi India Jonathan Trott 38 CapeTown SouthAfric Kumara Sangakkara 41 Matale Srilanka Virat Kohli 30 Delhi India Rohit Sharma 32 Nagpur India sqlite>
在SQLite中,預設情況下,列的寬度為 10 個值,超過此寬度的值將被截斷(觀察上表中第 2 行的國家/地區列)。您可以使用.width命令在檢索表內容之前將每列的寬度設定為所需的值,如下所示:
sqlite> .width 10, 10, 4, 10, 13 sqlite> SELECT * FROM CRICKETERS; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- ------------- Shikhar Dhawan 33 Delhi India Jonathan Trott 38 CapeTown SouthAfrica Kumara Sangakkara 41 Matale Srilanka Virat Kohli 30 Delhi India Rohit Sharma 32 Nagpur India sqlite>
使用 Python 檢索資料
對任何資料庫進行讀取操作意味著從資料庫中獲取一些有用的資訊。您可以使用 sqlite python 模組提供的 fetch() 方法從 MYSQL 中獲取資料。
sqlite3.Cursor 類提供了三種方法,即 fetchall()、fetchmany() 和 fetchone(),其中:
fetchall() 方法檢索查詢結果集中的所有行,並將其作為元組列表返回。(如果我們在檢索幾行後執行此操作,它將返回其餘行)。
fetchone() 方法獲取查詢結果中的下一行,並將其作為元組返回。
fetchmany() 方法類似於 fetchone(),但它檢索查詢結果集中的下一組行,而不是單個行。
注意 - 結果集是在使用遊標物件查詢表時返回的物件。
示例
以下示例使用 SELECT 查詢獲取 EMPLOYEE 表的所有行,並從獲得的結果集中首先使用 fetchone() 方法檢索第一行,然後使用 fetchall() 方法檢索其餘行。
以下 Python 程式演示瞭如何從上面示例中建立的 COMPANY 表中獲取和顯示記錄。
import sqlite3 #Connecting to sqlite conn = sqlite3.connect('example.db') #Creating a cursor object using the cursor() method cursor = conn.cursor() #Retrieving data cursor.execute('''SELECT * from EMPLOYEE''') #Fetching 1st row from the table result = cursor.fetchone(); print(result) #Fetching 1st row from the table result = cursor.fetchall(); print(result) #Commit your changes in the database conn.commit() #Closing the connection conn.close()
輸出
('Ramya', 'Rama priya', 27, 'F', 9000.0) [('Vinay', 'Battacharya', 20, 'M', 6000.0), ('Sharukh', 'Sheik', 25, 'M', 8300.0), ('Sarmista', 'Sharma', 26, 'F', 10000.0), ('Tripthi', 'Mishra', 24, 'F', 6000.0) ]