
- 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 - 更新表
在任何資料庫中,UPDATE 操作都意味著修改表中已存在的一個或多個記錄的值。可以使用 UPDATE 語句更新 SQLite 中現有記錄的值。
要更新特定行,需要結合使用 WHERE 子句。
語法
以下是 SQLite 中 UPDATE 語句的語法:
UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition];
示例
假設我們使用以下查詢建立了一個名為 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>
以下語句修改了名為 **Shikhar** 的板球運動員的年齡:
sqlite> UPDATE CRICKETERS SET AGE = 45 WHERE FIRST_NAME = 'Shikhar' ; sqlite>
如果檢索 FIRST_NAME 為 Shikhar 的記錄,則會觀察到年齡值已更改為 45:
sqlite> SELECT * FROM CRICKETERS WHERE FIRST_NAME = 'Shikhar'; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- ------------- Shikhar Dhawan 45 Delhi India sqlite>
如果未使用 WHERE 子句,則所有記錄的值都將被更新。以下 UPDATE 語句將 CRICKETERS 表中所有記錄的年齡增加 1:
sqlite> UPDATE CRICKETERS SET AGE = AGE+1; sqlite>
如果使用 SELECT 命令檢索表的內容,則可以看到更新後的值:
sqlite> SELECT * FROM CRICKETERS; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- ------------- Shikhar Dhawan 46 Delhi India Jonathan Trott 39 CapeTown SouthAfrica Kumara Sangakkara 42 Matale Srilanka Virat Kohli 31 Delhi India Rohit Sharma 33 Nagpur India sqlite>
使用 python 更新現有記錄
要向 SQLite 資料庫中的現有表新增記錄:
匯入 sqlite3 包。
使用 *connect()* 方法建立一個連線物件,並將資料庫名稱作為引數傳遞給它。
*cursor()* 方法返回一個遊標物件,您可以使用它與 SQLite3 通訊。透過在(上面建立的)連線物件上呼叫 cursor() 物件來建立一個遊標物件。
然後,透過將 UPDATE 語句作為引數傳遞給它,在遊標物件上呼叫 execute() 方法。
示例
下面的 Python 示例建立一個名為 EMPLOYEE 的表,向其中插入 5 條記錄,並將所有男性員工的年齡增加 1:
import sqlite3 #Connecting to sqlite conn = sqlite3.connect('example.db') #Creating a cursor object using the cursor() method cursor = conn.cursor() #Doping EMPLOYEE table if already exists. cursor.execute("DROP TABLE IF EXISTS EMPLOYEE") #Creating table as per requirement sql ='''CREATE TABLE EMPLOYEE( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT )''' cursor.execute(sql) #Inserting data cursor.execute('''INSERT INTO EMPLOYEE (FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Ramya', 'Rama priya', 27, 'F', 9000), ('Vinay', 'Battacharya', 20, 'M', 6000), ('Sharukh', 'Sheik', 25, 'M', 8300), ('Sarmista', 'Sharma', 26, 'F', 10000), ('Tripthi', 'Mishra', 24, 'F', 6000)''') conn.commit() #Fetching all the rows before the update print("Contents of the Employee table: ") cursor.execute('''SELECT * from EMPLOYEE''') 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: ") cursor.execute('''SELECT * from EMPLOYEE''') 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), ('Vinay', 'Battacharya', 21, 'M', 6000.0), ('Sharukh', 'Sheik', 26, 'M', 8300.0), ('Sarmista', 'Sharma', 26, 'F', 10000.0), ('Tripthi', 'Mishra', 24, 'F', 6000.0)]
廣告