Determining modifying statements from non-modifying in SQLite
What is the most reliable way to determine what statements are "querying" versus "modifying"? For example, SELECT
versus UPDATE / INSERT / CREATE
.
Parsing the statement myself seems the obvious first attempt, but I can't help but think that this would be a flaky solution. Just looking for SELECT
at the beginning doesn't work, as PRAGMA can also return results, and I'm sure there are a multitude of ways that strategy could fail. Testing for z开发者_开发问答ero rows returned from the cursor doesn't work either, as a SELECT can obviously return zero results.
I'm working with SQLite via the Python sqlite3 module.
Use the sqlite3_changes API call, which is also available from SQL using the changes function.
As TokenMacGuy mentioned, you can rollback the transaction containing the statement that caused the changes; the sqlite3_changes
function will let you know if that is necessary.
There is also the update_hook callback if you need more fine grained information abouth the tables and rows affected.
精彩评论