Database Locking Error when trying to acces SQLite back to back

You must Login before you can answer or comment on any questions.

Sometimes when I try to do two SQL functions back to back, i.e.: select then immediately insert, or delete then select, etc, I get this error:

"Error occurred calling next on a PLSqliteResultSet. SQLite error: 'database is locked' for 'INSERT INTO groups (groupname) VALUES (?)'";

Based on what I've been able to find, it is happening because the first hit to the DB is not complete before trying to do the second one. Although I understand why it is happening, I am not sure how to prevent it. Originally I had been doing the back to back functions on the same connection. Then I tried closing the first connection and opening a new one. That didn't solve the problem.

Is there a best-practice way to precent this error?

Application type: mobile. Titanium SDK: 1.8.2. Platform & version: iOS 5.x. Device: iOS simulator. Host Operating System: OSX 10.7.3. Titanium Studio: 2.0.1

— asked 1 year ago by Casey Crookston
1 Comment
  • Hello! I'm facing the same problem here. Did you finally find the solution yet? thank you!

    — commented 7 months ago by wei huang

3 Answers

Accepted Answer

i don't think commits around select statements do anything? I do see you haven't closed nameCount though. that should be closed as well

var nameCount = dbc.execute('SELECT count (*) AS rowCount FROM groups WHERE groupname = ?', e.values.GroupName);
var count = nameCount.fieldByName('rowCount');
nameCount.close();
dbc.close();

— answered 1 year ago by Paul Hamilton
answer permalink
5 Comments
  • make sure you close everything in your other calls as well elsewhere. I don't think the problem lies with this piece of code. I would bet you are calling the table 'GroupTextB' elsewhere. (perhaps with a select statement?) and not closing its variable / database.

    — commented 1 year ago by Paul Hamilton

  • Dang! I really thought we were on to something . I closed nameCount but still got the same error :-(

    — commented 1 year ago by Casey Crookston

  • Yeah, I have gone over everything and checked for closes. But I'll check again.

    — commented 1 year ago by Casey Crookston

  • Show 2 more comments

try using db.close() after each statement. Could also make sure your inserts/deletes are completed by using

db.execute('BEGIN TRANSACTION');
    INSERTS/DELETES HERE
db.execute('COMMIT TRANSACTION');

— answered 1 year ago by Paul Hamilton
answer permalink
1 Comment
  • Thanks Paul. Implementing your suggestion, here's what I've got now. I'm still getting the Database Is Locked error:

    var dbc = Titanium.Database.open('myDB');
    dbc.execute('BEGIN TRANSACTION');
    var nameCount = dbc.execute('SELECT count (*) AS rowCount FROM groups WHERE groupname = ?', e.values.GroupName);
    dbc.execute('COMMIT TRANSACTION');
    var count = nameCount.fieldByName('rowCount');
    dbc.close();
    if (count===0)
    {
        var dbc2 = Titanium.Database.open('GroupTextB');
        dbc2.execute('BEGIN TRANSACTION');
        dbc2.execute('INSERT INTO groups (groupname) VALUES (?)', e.values.GroupName);
        dbc2.execute('COMMIT TRANSACTION');  // <--- Error on this line
        dbc2.close();
    }
    else
    {
        // other stuff
    }
    The error occurs on the second COMMIT TRANSACTION. Not sure what else to try here.

    — commented 1 year ago by Casey Crookston

I have the same problem here! Any update about this problem? thank you!

Your Answer

Think you can help? Login to answer this question!