Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

xcode - sqlite3 - iOS - database is locked

I am developing an app for ipad and i am using sqlite sentences (select, update, insert, delete).

I open (sqlite3_open) the database at the beginning and close (sqlite3_close) at the end of each sentence. But sometimes i′ve got the "database is locked" message.

I don′t know what can i do to solve this.

Thanks and sorry for this little information.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I'm not mistaken , the problem with sqllite is that you can only access it once at a time. If you have multiple threads, you can run in this situation. Example:

Run method1 (which accesses the database) on thread t1. Run method2 (which accesses the database) on thread t2 after x seconds.

If method1 is not finished in those x seconds , both methods will access it at the same time. And , as I said , I know that sqllite does not support this.

You should try to flag the usage of your database and if you want to access it but it is in use , try again after x seconds. Like this:

- (void) generalMethodThatUsesDatabses
{
    if(databaseIsUsed)
    {
         [self performSelector:@selector(generalMethodThatUsesDatabses) withObject:nil afterDelay:5];
          return;
    }

    databaseIsUsed = TRUE;   //global bool variable


    //your code here

    databaseIsUsed = FALSE;

}

Hope this helps. Cheers!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...