DELETE All Rows FROM TABLE - Python sqlite3 Examples
Python - Delete All Rows from Sqlite Table
To delete all rows from Sqlite3 table, you can execute SQL DELETE query.
In this tutorial, we shall learn how to delete all rows or records from a Table of Sqlite Database using sqlite3 library.
Steps to Delete All Rows of Sqlite Table
The detailed steps to delete rows from sqlite3 table are:
- Make a connection to sqlite3 database.
- Get cursor from the connection.
- Execute
DELETE FROM table
query.
Examples
1. Delete rows from sqlite3 table
In this example, we will learn how to remove or delete all the records from a sqlite3 database table using DELETE FROM TABLE query.
Python Program
import sqlite3
conn = sqlite3.connect('mysqlite.db')
c = conn.cursor()
# Delete all rows from table
c.execute('DELETE FROM students;',);
print('We have deleted', c.rowcount, 'records from the table.')
# Commit the changes to db
conn.commit()
# Close the connection
conn.close()
Explanation of the Code:
- Creating Connection and Cursor: The connection to the database is created using
sqlite3.connect('mysqlite.db')
, and a cursor object is obtained withconn.cursor()
. - Executing the DELETE Query: The query
'DELETE FROM students;'
deletes all rows from thestudents
table. Theexecute()
method is used to run the query. - Commit Changes: After the query is executed,
conn.commit()
is called to save the changes to the database. - Closing the Connection: Finally, we close the database connection using
conn.close()
to free up resources.
2. Delete all rows with a condition
In some cases, you might want to delete rows based on a specific condition. In the example below, we will delete all students with a specific grade.
Python Program
import sqlite3
conn = sqlite3.connect('mysqlite.db')
c = conn.cursor()
# Delete students with grade 7
c.execute('DELETE FROM students WHERE grade = 7;')
print('We have deleted', c.rowcount, 'records from the table.')
# Commit the changes to db
conn.commit()
# Close the connection
conn.close()
Explanation of the Code:
- Condition in DELETE Query: The query
'DELETE FROM students WHERE grade = 7;'
deletes only those students whose grade is equal to 7. This is an example of how you can use conditions in your DELETE queries. - Checking Affected Rows: The
c.rowcount
shows the number of rows that were deleted, and in this case, it will show the number of students with grade 7 that were removed.
3. Delete rows from multiple tables (JOIN query)
If you need to delete rows from multiple related tables, you can perform a delete operation using a join. Below is an example where we delete students from the "students" table where the student ID matches a specific condition from a related table "enrollments".
Python Program
import sqlite3
conn = sqlite3.connect('mysqlite.db')
c = conn.cursor()
# Delete students who are not enrolled in any courses
c.execute('''
DELETE FROM students WHERE id NOT IN (
SELECT student_id FROM enrollments
);''')
print('We have deleted', c.rowcount, 'records from the table.')
# Commit the changes to db
conn.commit()
# Close the connection
conn.close()
Explanation of the Code:
- JOIN in DELETE Query: The query uses a subquery to identify students who are not enrolled in any courses by comparing the student IDs between the "students" table and the "enrollments" table.
- Deleting with JOIN: The outer query deletes rows from the "students" table where the condition in the subquery is met, in this case, students who are not enrolled in any courses.
- Commit and Close: As before, after executing the delete query, the changes are committed, and the connection is closed.
Summary
In this tutorial, we learned how to delete rows from an sqlite3 table using the following techniques:
- Deleting all rows from a table with a basic
DELETE FROM
query. - Using conditions in the
DELETE
query to delete specific rows. - Deleting rows using a JOIN between multiple tables to remove related data.
These methods allow you to efficiently manage and clean up your SQLite tables when using Python.