开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜