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

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 moduleimport sqlite3  # create connection to the database # my_databaseconnection = sqlite3.connect('my_database.db')  # create table named address of customers # with 4 columns id,name age and addressconnection.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 connectionconnection.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 databaseimport sqlite3  # create connection to the database# my_databaseconnection = sqlite3.connect('my_database.db')  # insert query to insert valuesconnection.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 tablecursor = connection.execute("SELECT * from ship")print("Actual data")  # display row by rowfor row in cursor:    print(row)  # query to delete all data where ship_id = 2connection.execute("DELETE from ship where ship_id=2")  print("After  deleting ship id = 2 row")  # display row by rowcursor = connection.execute("SELECT * from ship")for row in cursor:    print(row)  # close the connectionconnection.close()
Output:
Example 2:
In this example delete data where the ship address is hyderabad on the same table.
Python3
# import sqlite module databaseimport sqlite3  # create connection to the database# my_databaseconnection = sqlite3.connect('my_database.db')  # insert query to insert valuesconnection.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 tablecursor = connection.execute("SELECT * from ship")print("Actual data")  # display row by rowfor row in cursor:    print(row)  # query to delete all data where ship_id = 2connection.execute("DELETE from ship where ship_destination='hyderabad'")  print("After  deleting ship address = hyderabad row")  # display row by rowcursor = connection.execute("SELECT * from ship")for row in cursor:    print(row)  # close the connectionconnection.close()
Output:
Picked
Python-SQLite
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python String | replace()
Reading and Writing to text files in Python
*args and **kwargs in Python
Convert integer to string in Python
Create a Pandas DataFrame from Lists
Check if element exists in list in Python