Python PostgreSQL - 資料選擇



您可以使用 SELECT 語句檢索 PostgreSQL 中現有表的內容。在此語句中,您需要指定表名,它將以表格格式返回其內容,這稱為結果集。

語法

以下是 PostgreSQL 中 SELECT 語句的語法:

SELECT column1, column2, columnN FROM table_name;

示例

假設我們使用以下查詢建立了一個名為 CRICKETERS 的表:

postgres=# CREATE TABLE CRICKETERS (
   First_Name VARCHAR(255), Last_Name VARCHAR(255), 
   Age int, Place_Of_Birth VARCHAR(255), Country VARCHAR(255)
);
CREATE TABLE
postgres=#

如果我們使用 INSERT 語句向其中插入了 5 條記錄,如下所示:

postgres=# insert into CRICKETERS values('Shikhar', 'Dhawan', 33, 'Delhi', 'India');
INSERT 0 1
postgres=# insert into CRICKETERS values('Jonathan', 'Trott', 38, 'CapeTown', 'SouthAfrica');
INSERT 0 1
postgres=# insert into CRICKETERS values('Kumara', 'Sangakkara', 41, 'Matale', 'Srilanka');
INSERT 0 1
postgres=# insert into CRICKETERS values('Virat', 'Kohli', 30, 'Delhi', 'India');
INSERT 0 1
postgres=# insert into CRICKETERS values('Rohit', 'Sharma', 32, 'Nagpur', 'India');
INSERT 0 1

以下 SELECT 查詢檢索 CRICKETERS 表中 FIRST_NAME、LAST_NAME 和 COUNTRY 列的值。

postgres=# SELECT FIRST_NAME, LAST_NAME, COUNTRY FROM CRICKETERS;
first_name  | last_name  | country
------------+------------+-------------
Shikhar     | Dhawan     | India
Jonathan    | Trott      | SouthAfrica
Kumara      | Sangakkara | Srilanka
Virat       | Kohli      | India
Rohit       | Sharma     | India
(5 rows)

如果要檢索每條記錄的所有列,需要將列名替換為 "*",如下所示:

postgres=# SELECT * FROM CRICKETERS;
first_name  | last_name  | age | place_of_birth | 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
(5 rows)
postgres=#

使用 Python 檢索資料

對任何資料庫進行讀取操作都意味著從資料庫中獲取一些有用的資訊。您可以使用 psycopg2 提供的 fetch() 方法從 PostgreSQL 中提取資料。

Cursor 類提供三種方法:fetchall()、fetchmany() 和 fetchone(),其中:

  • fetchall() 方法檢索查詢結果集中的所有行,並將其作為元組列表返回。(如果我們在檢索幾行後執行此操作,它將返回其餘的行)。

  • fetchone() 方法獲取查詢結果中的下一行,並將其作為元組返回。

  • fetchmany() 方法類似於 fetchone(),但是它檢索查詢結果集中的下一組行,而不是單行。

注意 - 結果集是在使用遊標物件查詢表時返回的物件。

示例

以下 Python 程式連線到 PostgreSQL 的名為 mydb 的資料庫,並檢索名為 EMPLOYEE 的表中的所有記錄。

import psycopg2

#establishing the connection
conn = psycopg2.connect(
   database="mydb", user='postgres', password='password', host='127.0.0.1', port= '5432'
)

#Setting auto commit false
conn.autocommit = True

#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)]
廣告