SQLite Tutorial

  • Home
  • Start Here
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • API
    • SQLite Python
    • SQLite Node.js
    • SQLite Java
    • SQLite PHP
  • Try It
Home / SQLite Python / SQLite Python: Deleting Data

SQLite Python: Deleting Data

Summary: this tutorial shows you how to delete data in the SQLite database from a Python program using the sqlite3 module.

In order to delete data in the SQLite database from a Python program, you use the following steps:

  1. First, establish a connection the SQLite database by creating a Connection object using the connect() function.
  2. Second, to execute a DELETE statement, you need to create a Cursor object using the cursor() method of the Connection object.
  3. Third, execute the  DELETE statement using the execute() method of the Cursor object. In case you want to pass the arguments to the statement, you use a question mark ( ?) for each argument.

The following  create_connection() function establishes a database connection to an SQLite database specified by a database file name:

def create_connection(db_file): """ create a database connection to the SQLite database specified by the db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect(db_file) except Error as e: print(e) return conn

The following delete_task() function deletes a task in the tasks table by id.

def delete_task(conn, id): """ Delete a task by task id :param conn: Connection to the SQLite database :param id: id of the task :return: """ sql = 'DELETE FROM tasks WHERE id=?' cur = conn.cursor() cur.execute(sql, (id,)) conn.commit()

The following delete_all_tasks() function deletes all rows in the tasks table.

def delete_all_tasks(conn): """ Delete all rows in the tasks table :param conn: Connection to the SQLite database :return: """ sql = 'DELETE FROM tasks' cur = conn.cursor() cur.execute(sql) conn.commit()

This main() function calls the create_connection() function and the delete_task() function to delete the task with id 2 from the tasks table:

def main(): database = r"C:\sqlite\db\pythonsqlite.db" # create a database connection conn = create_connection(database) with conn: delete_task(conn, 2); # delete_all_tasks(conn); if __name__ == '__main__': main()

Here is the full program:

import sqlite3 from sqlite3 import Error def create_connection(db_file): """ create a database connection to the SQLite database specified by the db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect(db_file) except Error as e: print(e) return conn def delete_task(conn, id): """ Delete a task by task id :param conn: Connection to the SQLite database :param id: id of the task :return: """ sql = 'DELETE FROM tasks WHERE id=?' cur = conn.cursor() cur.execute(sql, (id,)) conn.commit() def delete_all_tasks(conn): """ Delete all rows in the tasks table :param conn: Connection to the SQLite database :return: """ sql = 'DELETE FROM tasks' cur = conn.cursor() cur.execute(sql) conn.commit() def main(): database = r"C:\sqlite\db\pythonsqlite.db" # create a database connection conn = create_connection(database) with conn: delete_task(conn, 2); # delete_all_tasks(conn); if __name__ == '__main__': main()

Note that you can comment/ un-comment the function call in the main() function to test each delete function individually.

In this tutorial, you have learned how to delete data in the SQLite database from a Python program using the sqlite3 module.

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite Python: Updating Data
Next SQLite Python: Querying Data

Getting Started

  • What Is SQLite
  • Download & Install SQLite
  • SQLite Sample Database
  • SQLite Commands

SQLite Tutorial

  • SQLite Select
  • SQLite Order By
  • SQLite Select Distinct
  • SQLite Where
  • SQLite Limit
  • SQLite BETWEEN
  • SQLite IN
  • SQLite Like
  • SQLite IS NULL
  • SQLite GLOB
  • SQLite Join
  • SQLite Inner Join
  • SQLite Left Join
  • SQLite Cross Join
  • SQLite Self-Join
  • SQLite Full Outer Join
  • SQLite Group By
  • SQLite Having
  • SQLite Union
  • SQLite Except
  • SQLite Intersect
  • SQLite Subquery
  • SQLite EXISTS
  • SQLite Case
  • SQLite Insert
  • SQLite Update
  • SQLite Delete
  • SQLite Replace
  • SQLite Transaction

SQLite Data Definition

  • SQLite Data Types
  • SQLite Date & Time
  • SQLite Create Table
  • SQLite Primary Key
  • SQLite Foreign Key
  • SQLite NOT NULL Constraint
  • SQLite UNIQUE Constraint
  • SQLite CHECK Constraint
  • SQLite AUTOINCREMENT
  • SQLite Alter Table
  • SQLite Rename Column
  • SQLite Drop Table
  • SQLite Create View
  • SQLite Drop View
  • SQLite Index
  • SQLite Expression-based Index
  • SQLite Trigger
  • SQLite VACUUM
  • SQLite Transaction
  • SQLite Full-text Search

SQLite Tools

  • SQLite Commands
  • SQLite Show Tables
  • SQLite Describe Table
  • SQLite Dump
  • SQLite Import CSV
  • SQLite Export CSV

SQLite Functions

  • SQLite AVG
  • SQLite COUNT
  • SQLite MAX
  • SQLite MIN
  • SQLite SUM

SQLite Interfaces

  • SQLite PHP
  • SQLite Node.js
  • SQLite Java
  • SQLite Python

About SQLite Tutorial

SQLite Tutorial website helps you master SQLite quickly and easily. It explains the complex concepts in simple and easy-to-understand ways so that you can both understand SQLite fast and know how to apply it in your software development work more effectively.

Looking for a tutorial…

If you did not find the tutorial that you are looking for, you can use the following search box. In case the tutorial is not available, you can request for it using the request for a SQLite tutorial form.

Recent Tutorials

  • SQLite IIF
  • SQLite Generated Columns
  • SQLite Getting Started
  • SQLite Programming Interfaces
  • SQLite Concat
  • SQLite INSTEAD OF Triggers
  • SQLite Join
  • SQLite IS NULL

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2020 SQLite Tutorial. All Rights Reserved.