Open In App

How to Delete a Specific Row from SQLite Table using Python ?

Last Updated : 28 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to delete of a specific row from the SQLite table using Python.

In order to delete a particular row from a table in SQL, we use the DELETE query, The DELETE Statement in SQL is used to delete existing records from a table. We can delete a single record or multiple records depending on the condition we specify in the WHERE clause.

Syntax:

DELETE FROM table_name

WHERE condition;

We are going to create a table and then perform deletion operations in it.

Python3




# importing sqlite module
import sqlite3
  
# create connection to the database 
# my_database
connection = sqlite3.connect('my_database.db')
  
# create table named address of customers 
# with 4 columns id,name age and address
connection.execute('''CREATE TABLE ship (ship_id INT, ship_name \
TEXT NOT NULL, ship_destination CHAR(50) NOT NULL); ''')
  
print("Ship table created successfully")
  
# close the connection
connection.close()


Output:

Ship table created successfully

Example 1:

Python program to insert data and delete data where 2 is ship id.

Python3




# import sqlite module database
import sqlite3
  
# create connection to the database
# my_database
connection = sqlite3.connect('my_database.db')
  
# insert query to insert values
connection.execute("INSERT INTO ship  VALUES (1, 'tata-hitachi','noida' )")
connection.execute("INSERT INTO ship  VALUES (2, 'tata-mumbai','mumbai' )")
connection.execute("INSERT INTO ship  VALUES (3, 'tata-express','hyderabad' )")
  
# query to display all data in the table
cursor = connection.execute("SELECT * from ship")
print("Actual data")
  
# display row by row
for row in cursor:
    print(row)
  
# query to delete all data where ship_id = 2
connection.execute("DELETE from ship where ship_id=2")
  
print("After  deleting ship id = 2 row")
  
# display row by row
cursor = connection.execute("SELECT * from ship")
for row in cursor:
    print(row)
  
# close the connection
connection.close()


Output:

Example 2:

In this example delete data where the ship address is hyderabad on the same table.

Python3




# import sqlite module database
import sqlite3
  
# create connection to the database
# my_database
connection = sqlite3.connect('my_database.db')
  
# insert query to insert values
connection.execute("INSERT INTO ship  VALUES (1, 'tata-hitachi','noida' )")
connection.execute("INSERT INTO ship  VALUES (2, 'tata-mumbai','mumbai' )")
connection.execute("INSERT INTO ship  VALUES (3, 'tata-express','hyderabad' )")
  
# query to display all data in the table
cursor = connection.execute("SELECT * from ship")
print("Actual data")
  
# display row by row
for row in cursor:
    print(row)
  
# query to delete all data where ship_id = 2
connection.execute("DELETE from ship where ship_destination='hyderabad'")
  
print("After  deleting ship address = hyderabad row")
  
# display row by row
cursor = connection.execute("SELECT * from ship")
for row in cursor:
    print(row)
  
# close the connection
connection.close()


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads