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 zero 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_changesfunction 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.