[yunqa.de] Re: SQLite - Execute SQL with retry

  • From: Tobias Rapp <t.rapp@xxxxxxxxxxxxxxx>
  • To: yunqa@xxxxxxxxxxxxx
  • Date: Fri, 15 May 2015 09:32:38 +0200

On 13.05.2015 16:58, Delphi Inspiration wrote:

On 13.05.2015 11:43, Tobias Rapp wrote:

Are there any side-effects of re-executing a SQL statement like
INSERT, UPDATE or DELETE after sqlite3_exec returns SQLITE_LOCKED
(like some triggers executed twice, etc)?

No. SQLite is atomic: It executes SQL statements to completion or not
at all.

In your case, SQLITE_LOCKED indicates that sqlite3_step() did not
succeed and no changes have been written. So in order to achieve your
goal you have no other choice but to re-execute the statement when
the database is no longer locked.

I have now created a sub-class of TDISQLite3Database that contains a
re-implementation of the Execute method with included retry. Would be great if exec-retry could be provided by a future version of DISQLite3.

BTW: While I have been at it I also created a method "ExecuteWithParams( const Sql : Utf8String; const Params : array of const )" that simplifies code writing for a lot of ad-hoc statements in my project. For example:

fDatabase.ExecuteWithParams(
'UPDATE User SET LoginName=? WHERE UserId=?', [LoginName, UserId]);

Currently it only works for single-statement strings but if you are interested I can send my code to you directly.

Best regards,
Tobias

_______________________________________________
Delphi Inspiration mailing list
yunqa@xxxxxxxxxxxxx
//www.freelists.org/list/yunqa



Other related posts: