Android Question SqlLite - affected rows

walterf25

Expert
Licensed User
Longtime User
Is there any way to get the affected rows count for an update command?
I'm not sure you can get a count of rows updated after the rows being updated, however you can see the numbers of rows that need to be updated (before the update) based on the "WHERE" statement.

Cheers,
Walter
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Hi,
You can use changes() core function of SqLite : https://www.sqlite.org/lang_corefunc.html

changes() The changes() function returns the number of database rows that were changed or inserted or deleted by the most recently completed INSERT, DELETE, or UPDATE statement, exclusive of statements in lower-level triggers. The changes() SQL function is a wrapper around the sqlite3_changes() C/C++ function and hence follows the same rules for counting changes.


So code should be like this:

B4X:
SQLpi.ExecNonQuery("UPDATE " & MyTable & " SET fieldnum=4 WHERE fieldnum=2") ' Or whathever
Dim nbChanged As Int
nbChanged = SQLpi.ExecQuerySingleResult("SELECT changes() FROM " & MyTable)
Log(nbChanged)  ' how many records have been updated
Patrick
 
Last edited:
Upvote 0

GaryK4

Member
Licensed User
Longtime User
Hi,
You can use changes() core function of SqLite : https://www.sqlite.org/lang_corefunc.html

changes() The changes() function returns the number of database rows that were changed or inserted or deleted by the most recently completed INSERT, DELETE, or UPDATE statement, exclusive of statements in lower-level triggers. The changes() SQL function is a wrapper around the sqlite3_changes() C/C++ function and hence follows the same rules for counting changes.


So code should be like this:

B4X:
SQLpi.ExecNonQuery("UPDATE " & MyTable & " SET fieldnum=4 WHERE fieldnum=2") ' Or whathever
Dim nbChanged As Int
nbChanged = SQLpi.ExecQuerySingleResult("SELECT changes() FROM " & MyTable)
Log(nbChanged)  ' how many records have been updated
Patrick


I saw the function, but did not know how to use it.
This worked!

Thanks a lot!!
 
Upvote 0
Top