
- 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 - 刪除資料
要從 MySQL 表中刪除記錄,需要使用DELETE FROM語句。要刪除特定記錄,需要結合使用 WHERE 子句。
語法
以下是 MySQL 中 DELETE 查詢的語法:
DELETE FROM table_name [WHERE Clause]
示例
假設我們在 MySQL 中建立了一個名為 EMPLOYEES 的表,如下所示:
mysql> CREATE TABLE EMPLOYEE( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT ); Query OK, 0 rows affected (0.36 sec)
如果我們使用 INSERT 語句向其中插入了 4 條記錄,如下所示:
mysql> INSERT INTO EMPLOYEE VALUES ('Krishna', 'Sharma', 19, 'M', 2000), ('Raj', 'Kandukuri', 20, 'M', 7000), ('Ramya', 'Ramapriya', 25, 'F', 5000), ('Mac', 'Mohan', 26, 'M', 2000);
以下 MySQL 語句將刪除 FIRST_NAME 為“Mac”的員工記錄。
mysql> DELETE FROM EMPLOYEE WHERE FIRST_NAME = 'Mac'; Query OK, 1 row affected (0.12 sec)
如果檢索表的內容,您會看到只有 3 條記錄,因為我們刪除了一條。
mysql> select * from EMPLOYEE; +------------+-----------+------+------+--------+ | FIRST_NAME | LAST_NAME | AGE | SEX | INCOME | +------------+-----------+------+------+--------+ | Krishna | Sharma | 20 | M | 2000 | | Raj | Kandukuri | 21 | M | 7000 | | Ramya | Ramapriya | 25 | F | 5000 | +------------+-----------+------+------+--------+ 3 rows in set (0.00 sec)
如果在沒有 WHERE 子句的情況下執行 DELETE 語句,則將刪除指定表中的所有記錄。
mysql> DELETE FROM EMPLOYEE; Query OK, 3 rows affected (0.09 sec)
如果檢索表的內容,您將得到一個空集,如下所示:
mysql> select * from EMPLOYEE; Empty set (0.00 sec)
使用 Python 刪除表中的記錄
當您想從資料庫中刪除一些記錄時,需要 DELETE 操作。
要刪除表中的記錄:
匯入mysql.connector包。
使用mysql.connector.connect()方法建立一個連線物件,並將使用者名稱、密碼、主機(可選,預設:localhost)和資料庫(可選)作為引數傳遞給它。
透過在上面建立的連線物件上呼叫cursor()方法來建立一個遊標物件。
然後,透過將其作為引數傳遞給execute()方法來執行DELETE語句。
示例
下面的程式刪除 EMPLOYEE 表中 AGE 大於 20 的所有記錄:
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 print("Contents of the table: ") cursor.execute("SELECT * from EMPLOYEE") print(cursor.fetchall()) #Preparing the query to delete records sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (25) try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database conn.commit() except: # Roll back in case there is any error conn.rollback() #Retrieving data print("Contents of the table after delete operation ") cursor.execute("SELECT * from EMPLOYEE") print(cursor.fetchall()) #Closing the connection conn.close()
輸出
Contents of the table: [('Krishna', 'Sharma', 22, 'M', 2000.0), ('Raj', 'Kandukuri', 23, 'M', 7000.0), ('Ramya', 'Ramapriya', 26, 'F', 5000.0), ('Mac', 'Mohan', 20, 'M', 2000.0), ('Ramya', 'Rama priya', 27, 'F', 9000.0)] Contents of the table after delete operation: [('Krishna', 'Sharma', 22, 'M', 2000.0), ('Raj', 'Kandukuri', 23, 'M', 7000.0), ('Mac', 'Mohan', 20, 'M', 2000.0)]
廣告