title: Simplest way to retry SQLite query if DB is locked?
body: <p>I as not quite sure where to ask, I hope it is right here.</p>
  
  <p>What I search for is the simplest solution to retry a SQLite query if the DB is busy.
  I use quassel as my IRC client on an server and I want to move old logs to a separate DB to keep the one it uses small.
  The script I wrote to do that is:</p>
  
  <pre><code>CREATE TEMP TABLE delfrom (id integer,val integer);
  ATTACH '/home/irc/oldlog.db' as log;
  BEGIN IMMEDIATE;
  REPLACE INTO delfrom (id,val) select 1337,messageid from backlog where time &lt; strftime('%s', 'now','-14 days') ORDER BY messageid DESC LIMIT 1;
  INSERT INTO log.log (messageid,time,bufferid,type,flags,senderid,message) SELECT messageid,time,bufferid,type,flags,senderid,message FROM backlog WHERE messageid &lt; (SELECT val FROM delfrom where id=1337);
  DELETE FROM backlog WHERE messageid &lt; (SELECT val FROM delfrom where id=1337);
  PRAGMA incremental_vacuum;
  COMMIT;
  </code></pre>
  
  <p>And I run it using sqlite3 quassel-storage.sqlite &lt; movelog.sql</p>
  
  <p>The problem is, since quassel is running while this executes, sometimes the <code>BEGIN IMMEDIATE;</code> fails because the DB is locked.</p>
  
  <p>Can someone suggest me an easy way to change that setup so the query is just retried every few seconds until it works?
  I read that the python SQLite wrapper has that built in? Is there a special way I have to activate this, and more important, can I attach the second DB using python?
  There is a timeout parameter to <code>sqlite3.connect</code> but I am not quite sure how that works. Does Python lock the whole DB for writing on every connect? </p>
  
  <p>I am in no way fixated to using Python. The solution I'd prefer would be that sqlite3 returns 0 when this error occurs and then just wrap it in an loop in the shell, but that seems not to work.</p>