- Python Data Access - Discussion
- Python Data Access - Useful Resources
- Python Data Access - Quick Guide
- Python MongoDB - Limit
- Python MongoDB - Update
- Python MongoDB - Drop Collection
- Python MongoDB - Delete Document
- Python MongoDB - Sort
- Python MongoDB - Query
- Python MongoDB - Find
- Python MongoDB - Insert Document
- Python MongoDB - Create Collection
- Python MongoDB - Create Database
- Python MongoDB - Introduction
- Python SQLite - Cursor Object
- Python SQLite - Join
- Python SQLite - Limit
- Python SQLite - Drop Table
- Python SQLite - Delete Data
- Python SQLite - Update Table
- Python SQLite - Order By
- Python SQLite - Where Clause
- Python SQLite - Select Data
- Python SQLite - Insert Data
- Python SQLite - Create Table
- Python SQLite - Establishing Connection
- Python SQLite - Introduction
- Python PostgreSQL - Cursor Object
- Python PostgreSQL - Join
- Python PostgreSQL - Limit
- Python PostgreSQL - Drop Table
- Python PostgreSQL - Delete Data
- Python PostgreSQL - Update Table
- Python PostgreSQL - Order By
- Python PostgreSQL - Where Clause
- Python PostgreSQL - Select Data
- Python PostgreSQL - Insert Data
- Python PostgreSQL - Create Table
- Python PostgreSQL - Create Database
- Python PostgreSQL - Database Connection
- Python PostgreSQL - Introduction
- Python MySQL - Cursor Object
- Python MySQL - Join
- Python MySQL - Limit
- Python MySQL - Drop Table
- Python MySQL - Delete Data
- Python MySQL - Update Table
- Python MySQL - Order By
- Python MySQL - Where Clause
- Python MySQL - Select Data
- Python MySQL - Insert Data
- Python MySQL - Create Table
- Python MySQL - Create Database
- Python MySQL - Database Connection
- Python MySQL - Introduction
- Python Data Access - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python SQLite - Delete Data
To delete records from a SQLite table, you need to use the DELETE FROM statement. To remove specific records, you need to use WHERE clause along with it.
To update specific rows, you need to use the WHERE clause along with it.
Syntax
Following is the syntax of the DELETE query in SQLite −
DELETE FROM table_name [WHERE Clause]
Example
Assume we have created a table with name CRICKETERS using the following query −
sqpte> CREATE TABLE CRICKETERS ( First_Name VARCHAR(255), Last_Name VARCHAR(255), Age int, Place_Of_Birth VARCHAR(255), Country VARCHAR(255) ); sqpte>
And if we have inserted 5 records in to it using INSERT statements as −
sqpte> insert into CRICKETERS values( Shikhar , Dhawan , 33, Delhi , India ); sqpte> insert into CRICKETERS values( Jonathan , Trott , 38, CapeTown , SouthAfrica ); sqpte> insert into CRICKETERS values( Kumara , Sangakkara , 41, Matale , Srilanka ); sqpte> insert into CRICKETERS values( Virat , Kohp , 30, Delhi , India ); sqpte> insert into CRICKETERS values( Rohit , Sharma , 32, Nagpur , India ); sqpte>
Following statement deletes the record of the cricketer whose last name is Sangakkara .
sqpte> DELETE FROM CRICKETERS WHERE LAST_NAME = Sangakkara ; sqpte>
If you retrieve the contents of the table using the SELECT statement, you can see only 4 records since we have deleted one.
sqpte> SELECT * FROM CRICKETERS; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- ------------- Shikhar Dhawan 46 Delhi India Jonathan Trott 39 CapeTown SouthAfrica Virat Kohp 31 Delhi India Rohit Sharma 33 Nagpur India sqpte>
If you execute the DELETE FROM statement without the WHERE clause, all the records from the specified table will be deleted.
sqpte> DELETE FROM CRICKETERS; sqpte>
Since you have deleted all the records, if you try to retrieve the contents of the CRICKETERS table, using SELECT statement you will get an empty result set as shown below −
sqpte> SELECT * FROM CRICKETERS; sqpte>
Deleting data using python
To add records to an existing table in SQLite database −
Import sqpte3 package.
Create a connection object using the connect() method by passing the name of the database as a parameter to it.
The cursor() method returns a cursor object using which you can communicate with SQLite3 . Create a cursor object by invoking the cursor() object on the (above created) Connection object.
Then, invoke the execute() method on the cursor object, by passing an DELETE statement as a parameter to it.
Example
Following python example deletes the records from EMPLOYEE table with age value greater than 25.
import sqpte3 #Connecting to sqpte conn = sqpte3.connect( example.db ) #Creating a cursor object using the cursor() method cursor = conn.cursor() #Retrieving contents of the table print("Contents of the table: ") cursor.execute( SELECT * from EMPLOYEE ) print(cursor.fetchall()) #Deleting records cursor.execute( DELETE FROM EMPLOYEE WHERE AGE > 25 ) #Retrieving data after delete print("Contents of the table after delete operation ") cursor.execute("SELECT * from EMPLOYEE") print(cursor.fetchall()) #Commit your changes in the database conn.commit() #Closing the connection conn.close()
Output
Contents of the table: [( 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)] Contents of the table after delete operation [( Vinay , Battacharya , 21, M , 6000.0), ( Tripthi , Mishra , 24, F , 6000.0)]Advertisements