Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 28 Apr, 2021

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:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :