SQLite total_changes() 函数使用指南
SQLite total_changes() 函数返回从建立当前连接开始的 INSERT, UPDATE, 和 DELETE 语句影响的总行数。
total_changes() 语法
这里是 SQLite total_changes() 函数的语法:
total_changes()
参数
SQLite total_changes() 函数不需要任何参数。
返回值
SQLite total_changes() 函数返回一个整数,它是从建立当前连接开始的 INSERT, UPDATE, 和 DELETE 语句影响的总行数。
total_changes() 函数的结果是每次受影响行数的累加,而不是真正的行数。
total_changes() 实例
为了演示 SQLite total_changes() 函数的作用,我们使用以下语句创建一个表 test_total_changes:
CREATE TABLE test_total_changes (
id INTEGER PRIMARY KEY,
note VARCHAR(100)
);
让我们使用下面的语句插入 2 行,
INSERT INTO test_total_changes (id, note)
VALUES (1, 'Hello'), ('2', 'World');
让我们使用 SQLite total_changes() 函数获取当前连接中受影响的总行数:
SELECT total_changes();
total_changes()
---------------
2这里, 总影响行数为 2。这是因为刚刚 INSERT 语句插入了 2 行。
让我们使用 UPDATE 语句将更改 id 为 2 的数据行:
UPDATE test_total_changes
set note = 'Everyone'
WHERE id = 2;
让我们使用 SQLite total_changes() 函数获取当前连接中受影响的总行数:
SELECT total_changes();
total_changes()
---------------
3这里, 总影响行数为 3。这是因为 INSERT 语句影响了 2 行, UPDATE 语句影响了 1 行。
最后,让我们使用 DELETE 语句删除 test_total_changes 表中的所有的行:
DELETE FROM test_total_changes;
让我们使用 SQLite total_changes() 函数获取当前连接中受影响的总行数:
SELECT total_changes();
total_changes()
---------------
5这里, 总影响行数为 5。这是因为 INSERT 语句影响了 2 行, UPDATE 语句影响了 1 行, 并且 DELETE 语句影响了 2 行。