changes() function is a SQLite3 function that returns the number of rows that were modified, inserted, or deleted by the most recently completed INSERT, UPDATE, or DELETE statement.
sqlite> .open E:/my_db/my_db.db
sqlite> .tables
Output of above code is studentsqlite> DELETE FROM student WHERE class='Four';
sqlite> SELECT changes();
Output is 9 as 9 rows or records are deleted.
sqlite> UPDATE student set mark=mark+10 WHERE class='Six';
sqlite> SELECT changes();
Output is 7
sqlite> INSERT INTO `student`
...> (`id`, `name`, `class`, `mark`, `gender`) VALUES
...> (40,'ABCD','Five',55,'Male');
sqlite> SELECT changes();
Output is 1
SELECT TOTAL_CHANGES();
After deleting 9 records we get these outputs by using changes() and total_changes().
sqlite> SELECT CHANGES();
35
sqlite> DELETE FROM student WHERE class='Four';
sqlite> SELECT CHANGES();
9
sqlite> SELECT TOTAL_CHANGES();
44
UPDATE table_name SET pw='new_password' WHERE user='user_id';
Here is one sample
if (changes != 1) {
// Handle error
}
<?php
$my_conn = new SQLite3('../my_db.db'); // connect to database
$sql="UPDATE student SET mark=mark+10 WHERE class='Four'"
$result = $my_conn->exec($sql);
if ($result) {
echo 'Number of rows modified: ', $my_conn->changes();
}
?>
Output
Number of rows modified: 9
When there is no change in table we will get output of changes() as 0. $my_conn->changes() as 0 . $query="UPDATE student SET mark=mark+10 WHERE class='One'"
Using INSERT query to add record to table
<?php
$my_conn = new SQLite3('../my_db.db');
$sql="INSERT INTO `student`
(`id`, `name`, `class`, `mark`, `gender`) VALUES
(39, 'Abcd', 'Four', 88, 'Female'),
(40, 'XYZ', 'Five', 38, 'Male')";
$result = $my_conn->exec($sql);
if ($result) {
echo 'Number of rows added : ', $my_conn->changes();
}
?>
Output
Number of rows added : 2
Delete all records
<?php
$my_conn = new SQLite3('../my_db.db');
$sql="DELETE FROM student";
$result = $my_conn->exec($sql);
if ($result) {
echo 'Number of rows deleted : ', $my_conn->changes();
}
?>
Output
Number of rows deleted : 37
query="DELETE FROM student WHERE class='Four'" # Query to delete records
#query='DROP TABLE student' # Query to delete table
try:
r_set=my_conn.execute(query)
#print("No of Records deleted : ",r_set.rowcount)
print("Number of records deleted : ",my_conn.total_changes)
my_conn.commit()
except sqlite3.Error as my_error:
print("error: ",my_error)
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.