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.
# 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.
# 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.
# 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: