SQLite TOTAL_CHANGES() 函数返回自当前数据库连接打开以来由 INSERT、UPDATE 或 DELETE 语句引起的行更改数。
语法
TOTAL_CHANGES()
参数
无需参数。
返回值
返回行数由当前会话的数据库中的 INSERT、UPDATE 或 DELETE 引起的更改
示例:
考虑下面的示例,其中创建了一个名为 Employee 的表。创建表后,TOTAL_CHANGES()函数用于获取由于当前会话在数据库中使用 INSERT、UPDATE 或 DELETE 语句而更改的行数。
sqlite> CREATE TABLE Employee (
EmpID INTEGER PRIMARY KEY,
Name VARCHAR(50));
sqlite> SELECT TOTAL_CHANGES();
+-----------------+
| TOTAL_CHANGES() |
+-----------------+
| 0 |
+-----------------+
sqlite> INSERT INTO Employee(Name) Values ('John');
sqlite> INSERT INTO Employee(Name) Values ('Marry');
sqlite> SELECT TOTAL_CHANGES();
+-----------------+
| TOTAL_CHANGES() |
+-----------------+
| 2 |
+-----------------+
sqlite> INSERT INTO Employee(Name) Values ('Kim') , ('Jo');
sqlite> SELECT TOTAL_CHANGES();
+-----------------+
| TOTAL_CHANGES() |
+-----------------+
| 4 |
+-----------------+
sqlite> SELECT * FROM Employee;
+-------+-------+
| EmpID | Name |
+-------+-------+
| 1 | John |
| 2 | Marry |
| 3 | Kim |
| 4 | Jo |
+-------+-------+
sqlite> UPDATE Employee SET Name = 'Suresh' WHERE EmpID = 4;
sqlite> SELECT TOTAL_CHANGES();
+-----------------+
| TOTAL_CHANGES() |
+-----------------+
| 5 |
+-----------------+
sqlite> SELECT * FROM Employee;
+-------+--------+
| EmpID | Name |
+-------+--------+
| 1 | John |
| 2 | Marry |
| 3 | Kim |
| 4 | Suresh |
+-------+--------+
sqlite> DELETE FROM Employee WHERE EmpID = 4;
sqlite> SELECT TOTAL_CHANGES();
+-----------------+
| TOTAL_CHANGES() |
+-----------------+
| 6 |
+-----------------+
sqlite> SELECT * FROM Employee;
+-------+-------+
| EmpID | Name |
+-------+-------+
| 1 | John |
| 2 | Marry |
| 3 | Kim |
+-------+-------+