Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

SQL Locked Error

I have a db.execute command running that errors every time it is run for the first time:

sqlSelect = db.execute('INSERT INTO…');
sqlSelect.close();

Even if I put a try {} catch(e){} around it I still get an error.

Any ideas?

— asked September 2nd 2010 by John Welch
  • error
  • locked
  • sql
0 Comments

3 Answers

  • Did you open the database before executing?

    I've used this function for all my db insert, delete, update and whatno without any issues. (iphone sdk)

    function insertData(){
      var db = Titanium.Database.open('some_db');
            db.execute('INSERT INTO .....');
            db.close(); 
    }
    
    — answered September 2nd 2010 by Daniel Lim
    permalink
    0 Comments
  • Yes, I guess I could open and close the database every time. I just didn't want to do that every time, but I guess if that fixes it…

    — answered September 2nd 2010 by John Welch
    permalink
    0 Comments
  • Thanks for the tip that worked.

    I will make sure to open and close the database every time it is used from now on. (I don't know why I didn't do that already but that fixed it.)

    — answered September 2nd 2010 by John Welch
    permalink
    1 Comment
    • The database file will lock when a write action is being performed such as a insert, delete or update. You'll need to ensure that you close and/or end the transaction of any insert/delete/update queries to prevent a locking run-time error. It's also worth noting that currently a try-catch around the query execution will not catch any SQL error notice.

      Docs: "while the database supports concurrent read access, only one user may write to it at a time. This is because a filesystem lock is placed on the file during write operations. This is an important point to bear in mind with multi-threaded applications" – http://wiki.appcelerator.org/display/guides/Working+with+Local+Data

      — commented September 13th 2011 by Dan Boorn
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.