Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python SQLite connection.total_changes() Function



The Pythonconnection.total_changes function returns the cumulative number of changes made to the database from the specific connection.

In SQLite, a connection refers to an active link between the application and database. This function allows us to execute SQL commands and queries in the database.

The total_changes attribute represents the total number of rows that have been modified since the connection was opened. This function is useful for tracking changes in the database.

Syntax

Following is the syntax for theconnection.total_changes() function.

conn.total_changes()

Return Value

This function returns an integer that represents the cumulative number of rows.

Example 1

In the below example, we will insert three rows into the table using theconnection.table_changes() function will return the total number of rows inserted.

import sqlite3conn = sqlite3.connect(':memory:')conn.execute('CREATE TABLE test(id INTEGER)')conn.execute('INSERT INTO test(id) VALUES (1), (2), (3)')print(conn.total_changes)

Output

The executed output is as follows −

3

Example 2

We are inserting two rows and updating one row in the database. Theconnection.total_changes() function will give the output as the total number of changes made in the database.

import sqlite3conn = sqlite3.connect(':memory:')conn.execute('CREATE TABLE test (id INTEGER, value TEXT)')conn.execute('INSERT INTO test (id, value) VALUES (1, "A"), (2, "B")')conn.execute('UPDATE test SET value = "C" WHERE id = 1')print(conn.total_changes)

Output

The result is obtained as follows −

3

Example 3

We are inserting and deleting the values from the database using theconnection.total_changes() function.

import sqlite3conn = sqlite3.connect(':memory:')conn.execute('CREATE TABLE test (id INTEGER)')conn.execute('INSERT INTO test (id) VALUES (1)')conn.execute('DELETE FROM test WHERE id = 1')print(conn.total_changes)

Output

We will get the output as follows −

2
python_modules.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp