sqlite3 - iOS - 数据库被锁定

sqlite3  -  iOS  - 数据库被锁定

问题描述:

我正在为ipad开发一个应用程序,我正在使用sqlite语句(选择,更新,插入,删除)。

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

我打开(sqlite3_open)数据库在每个句子的结尾处开始和结束(sqlite3_close)。但有时候我会收到数据库被锁定的消息。

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.

如果我没有弄错的话,问题是sqllite是您一次只能访问一次。
如果你有多个线程,你可以在这种情况下运行。示例:

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:

在线程t1上运行method1(访问数据库)。
在x秒之后在线程t2上运行method2(访问数据库)。

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

如果在这些x秒内没有完成method1,则两个方法都将在同一时间。
而且,正如我所说,我知道sqllite不支持这个。

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.

您应该尝试标记数据库的使用情况以及是否要访问但是它正在使用中,在x秒后再试一次。像这样:

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!