Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count total number of changes made after connecting SQLite to Python

  • Last Updated : 09 May, 2021

In this article, we are going to see how to count total changes since the SQLite database connection is open using Python. To get the total number of changes we use the connection object’s total_changes property.

Class Instance: sqlite3.Connection

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Syntax: <connection_object>.total_changes



Return Value: Total no. of rows inserted, deleted, updated since the database connection is open.

For the demonstration purpose, we would be using person table stored in geeks.db database.

Table Definition:

CREATE TABLE person(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER NOT NULL);

Example:

Python3




# Complete python program to get 
# the total no. of change since the
# beginning of the database connection.
  
# Import sqlite3 module to work with
# SQLite using python.
import sqlite3
  
# Create connection object by connecting
# to the required database (here geeks.db)
con = sqlite3.connect('c:/sqlite/db/geeks.db')
  
# SQL string to Create a database table
# named person.
create_table = '''CREATE TABLE person(
                  id INTEGER PRIMARY KEY AUTOINCREMENT,
                  name TEXT NOT NULL,
                  age INTEGER NOT NULL
                  );'''
  
# Execute the above SQL query.
con.execute(create_table)
  
# Print the current total no. of changes.
print("Total changes inititally:")
print(f'total_changes = {con.total_changes}\n')
  
# SQL string to insert records into 
# the table named person.
insert_data = '''INSERT INTO person(name, age)
                  VALUES ("Yogesh",21),
                  ("Vishal", 22),
                  ("Ajit",22),
                  ("Ashish",21),
                  ("Tanvi", 20);'''
  
# Execute the above SQL query.
con.execute(insert_data)
  
# Print the current total no. of changes.
print("Total changes after inserting 5 rows:")
print(f'total_changes = {con.total_changes}\n')
  
# SQL string to Select (retrieve) records
# from a database table named person.
select_data = 'SELECT * FROM  person;'
  
# Execute the above SQL query.
cursor = con.execute(select_data)
  
# Create a list of column names of the
# database table named person.
header = [d[0] for d in cursor.description]
  
# Print the column names separated 
# by a single space.
print(*header)
  
# Print the retrieved data.
for row in cursor:
  print(*row)
print()
  
# SQL string to delete a record from a
# database table named person.
delete_data = 'DELETE FROM person WHERE name="Tanvi";'
  
# Execute the above SQL query.
con.execute(delete_data)
  
# Print the current total no. of changes.
print("Total changes after deleting a row:")
print(f'\ntotal_changes = {con.total_changes}\n')
  
# Retrieve the modified (here deleted
# one record/row) data from a database
# table named person.
cursor = con.execute('SELECT * FROM person;')
  
# Print the retrieved data.
print(*header)
for row in cursor:
  print(*row)
print()
  
# Commit the changes to persist the
# changes.
con.commit()
  
# Close the database connection.
con.close()

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!