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:

  1. Make a connection to sqlite3 database.
  2. Get cursor from the connection.
  3. 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()

Output

We have deleted 18 records from the table.

Thats right. We have deleted 18 rows, which are all of them in that table.

Summary

In this tutorial of Python Examples, we learned to delete rows from sqlite3 table with the help of well detailed examples.