- Python PostgreSQL 教程
- 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 PostgreSQL 有用資源
- Python PostgreSQL - 快速指南
- Python PostgreSQL - 有用資源
- Python PostgreSQL - 討論
Python PostgreSQL - 更新表
您可以使用 UPDATE 語句修改 PostgreSQL 中表中現有記錄的內容。要更新特定的行,您需要與之一起使用 WHERE 子句。
語法
以下是 PostgreSQL 中 UPDATE 語句的語法:
UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition];
示例
假設我們使用以下查詢建立了一個名為 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
以下語句修改了名為Shikhar 的板球運動員的年齡:
postgres=# UPDATE CRICKETERS SET AGE = 45 WHERE FIRST_NAME = 'Shikhar' ; UPDATE 1 postgres=#
如果您檢索 FIRST_NAME 為 Shikhar 的記錄,您會發現年齡值已更改為 45:
postgres=# SELECT * FROM CRICKETERS WHERE FIRST_NAME = 'Shikhar'; first_name | last_name | age | place_of_birth | country ------------+-----------+-----+----------------+--------- Shikhar | Dhawan | 45 | Delhi | India (1 row) postgres=#
如果您沒有使用 WHERE 子句,則所有記錄的值都將更新。以下 UPDATE 語句將 CRICKETERS 表中所有記錄的年齡增加 1:
postgres=# UPDATE CRICKETERS SET AGE = AGE+1; UPDATE 5
如果您使用 SELECT 命令檢索表的內容,您可以看到更新後的值,如下所示:
postgres=# SELECT * FROM CRICKETERS; first_name | last_name | age | place_of_birth | country ------------+------------+-----+----------------+------------- Jonathan | Trott | 39 | CapeTown | SouthAfrica Kumara | Sangakkara | 42 | Matale | Srilanka Virat | Kohli | 31 | Delhi | India Rohit | Sharma | 33 | Nagpur | India Shikhar | Dhawan | 46 | Delhi | India (5 rows)
使用 Python 更新記錄
psycopg2 的遊標類提供了一個名為 execute() 的方法。此方法接受查詢作為引數並執行它。
因此,要使用 python 將資料插入 PostgreSQL 中的表中:
匯入psycopg2 包。
使用connect() 方法建立一個連線物件,將使用者名稱、密碼、主機(可選,預設值:localhost)和資料庫(可選)作為引數傳遞給它。
透過將 false 作為值設定為屬性autocommit 來關閉自動提交模式。
psycopg2 庫的Connection 類的cursor() 方法返回一個遊標物件。使用此方法建立一個遊標物件。
然後,透過將其作為引數傳遞給 execute() 方法來執行 UPDATE 語句。
示例
以下 Python 程式碼更新 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()
#Fetching all the rows before the update
print("Contents of the Employee table: ")
sql = '''SELECT * from EMPLOYEE'''
cursor.execute(sql)
print(cursor.fetchall())
#Updating the records
sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = 'M'"
cursor.execute(sql)
print("Table updated...... ")
#Fetching all the rows after the update
print("Contents of the Employee table after the update operation: ")
sql = '''SELECT * from EMPLOYEE'''
cursor.execute(sql)
print(cursor.fetchall())
#Commit your changes in the database
conn.commit()
#Closing the connection
conn.close()
輸出
Contents of the Employee table:
[
('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)
]
Table updated......
Contents of the Employee table after the update operation:
[
('Ramya', 'Rama priya', 27, 'F', 9000.0),
('Sarmista', 'Sharma', 26, 'F', 10000.0),
('Tripthi', 'Mishra', 24, 'F', 6000.0),
('Vinay', 'Battacharya', 21, 'M', 6000.0),
('Sharukh', 'Sheik', 26, 'M', 8300.0)
]
廣告