Skip to main content

How to delete data from database of SQLite3 database engine in Python

hi, how are you today




Till now we discussed
How  to create database in sqlite3 database engine in Python
How  to create table in database in sqlite3 database engine in Python.
How  to insert data in database in sqlite3 database engine in Python
How  to read/search data from database in sqlite3 database engine in Python
How  to update data in database in sqlite3 database engine in Python

So today, taking one more step further, we will discuss how to delete data from table inside database of SQLite3 database engine.

We are continuing with the same example of student database that we have discussed in previous post of creating table and inserting data.

Deleting Data from database
If you want to delete data then you can delete only one at a time. Anyhow if you want to delete the whole data then we can go with loop to delete all data one by one

As explained earlier, curs is optional, not mandatory while creating table and inserting data but mandatory for reading, updating and deleting data.

Syntax/Query to read all data from database
 curs.execute("delete from table_name where searchable_column_name=?",(searchable_column_name data_,))  

Here  column_name is the name of the column,which you want to delete.
Searchable_column_name is nothing but the location of the row which you want to delete example the primary key value( employee id , bank account).
The data matched with searchable_column_value_data, there we have to delete the mentioned column_name.

Below example will give you more clarity about searchable_data

combine code for how to read all data from database

1:  s_id=input("enter student id to delete: ")  
2:  import sqlite3 as sql  
3:  conn=sql.connect("mydatabase.db")  
4:  curs=conn.cursor()  
5:  curs.execute("delete from St_data where Student_id=?",(s_id,))  
6:  conn.commit()  
7:  conn.close()  
8:  print("deleted")  

[*NOTE: deletion will give you a successfully delete the data if data to be deleted is present. Anyhow if data to be deleted is not present still it will raise exception, so better way is to search thr data first and if present then delete else not]

Do not forget to save the data if you do any type of modification in table of database, i.e. inserting,updating and deleting. And to save use
 conn.commit()  

Always remember to close the connection before closing or quitting your software
 conn.close()  
This is the simple way to delete required data from database? 

So by this we have completed all four operations of SQLite3 database engine in Python.
In next post we will discuss a program which contains all operations in a single file

Hope you all like this post and I wish this proves useful to you.
Keep sharing & give your views about post in comments.
Comments are always welcomed for better improvement.

Thanks :)

Comments

Translate to your language

Popular posts from this blog

Different Stages of I2C communication

Different Stages Of I2C

How to import Pygame in pycharm

hi, how are you today

Working demonstration of UART communication using 2 8051 uController on Proteus with Embedded C code.

Here we are going to use 2 at89c51 uControllers just to understand UART communication in 8051.