iOS'数据库中的Sqlite被锁定'错误

时间:2021-05-01 15:26:32

I am using sqlite db for my iOS app. Here i get an error saying 'database is locked' while I am trying to insert a row in a table. I agree there are many questions posted for the same and many solutions and reasons given for the error but mine is not solved with any of those. Reasons being multithreading in sqlite is prohibited, database should be open, finalize should be called etc. I am posting my code to make it more transparent.

我正在为我的iOS应用程序使用sqlite db。在我尝试在表中插入一行时,我收到一条错误,说“数据库已被锁定”。我同意针对错误提供了相同的许多解决方案和原因的许多问题,但我的任何一个都没有解决。禁止在sqlite中进行多线程处理的原因,数据库应该打开,应该调用finalize等。我发布我的代码以使其更透明。

Insert Function

 int open=sqlite3_open([@"/Users/macintosh/Documents/Apps/Project/MyLocal DB/MyLocal.sqlite" UTF8String], &database);

sqlite3_stmt *statement = NULL;

const char *sql = "insert into History (MNo,PID, Move,Result,WID, CreatedOn) Values( ?, ?, ?, ?, ?, ?)";


NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];


if(open==SQLITE_OK)
{

    if (sqlite3_prepare(database, sql, -1, &statement, NULL) ==SQLITE_OK)
    {
        sqlite3_bind_int(statement, 0, mid);
        sqlite3_bind_int(statement, 1, pid);
        sqlite3_bind_int(statement, 2, cno);
        sqlite3_bind_int(statement, 3, result);
        sqlite3_bind_int(statement, 4, wid);
        sqlite3_bind_text(statement, 5, [[DateFormatter stringFromDate:[NSDate date]] UTF8String], -1, SQLITE_TRANSIENT);



        if (sqlite3_step(statement)==SQLITE_DONE)
        {
            NSLog(@"inserted the values in table");

        }
        else
        {
            NSLog(@" not inserted the values in table");
            NSLog(@"error: %s", sqlite3_errmsg(database));
        }
        sqlite3_finalize(statement);



    }
    else
    {
        NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(database));
    }
    sqlite3_close(database);
    NSLog(@"db closed");
}
else
{
    NSLog(@"An error has occured: %s",sqlite3_errmsg(database));
}

Note: I have used same function at many places to insert a row and it works well. I got this error now and I have made sure there is no other statement running on the same database while this statement is being run.

注意:我在很多地方使用了相同的功能来插入一行,效果很好。我现在收到此错误,并确保在运行此语句时没有其他语句在同一数据库上运行。

3 个解决方案

#1


4  

I think you forgot to close this database in any where that's why you are receiving this error

我想您忘记在任何地方关闭此数据库,这就是您收到此错误的原因

#2


2  

Often when you get this kind of error you have parallell processes in your app trying to access the same database at the same time. If that's the case, try to put:

通常,当您遇到此类错误时,您的应用程序中的并行进程会尝试同时访问同一个数据库。如果是这种情况,请尝试:

sqlite3_busy_timeout(database, 500);

somewhere between sqlite3_open and sqlite3_prepare in your code.

代码中的sqlite3_open和sqlite3_prepare之间的某处。

Then the database-connection will try to read/write in 500 milliseconds before it gives up, which is usually enough time to escape the locking.

然后数据库连接将在放弃之前500毫秒内尝试读/写,这通常足以逃脱锁定。

#3


1  

There are two reasons for this:

有两个原因:

  1. You may have forgotten to close database after query execution. You need to close database after every execution.
  2. 您可能忘记在查询执行后关闭数据库。每次执行后都需要关闭数据库。

  3. You may be executing multiple queries simultaneously. In case you execute multiple queries simultaneously, you must put some delay between multiple query executions.
  4. 您可能同时执行多个查询。如果同时执行多个查询,则必须在多个查询执行之间放置一些延迟。

#1


4  

I think you forgot to close this database in any where that's why you are receiving this error

我想您忘记在任何地方关闭此数据库,这就是您收到此错误的原因

#2


2  

Often when you get this kind of error you have parallell processes in your app trying to access the same database at the same time. If that's the case, try to put:

通常,当您遇到此类错误时,您的应用程序中的并行进程会尝试同时访问同一个数据库。如果是这种情况,请尝试:

sqlite3_busy_timeout(database, 500);

somewhere between sqlite3_open and sqlite3_prepare in your code.

代码中的sqlite3_open和sqlite3_prepare之间的某处。

Then the database-connection will try to read/write in 500 milliseconds before it gives up, which is usually enough time to escape the locking.

然后数据库连接将在放弃之前500毫秒内尝试读/写,这通常足以逃脱锁定。

#3


1  

There are two reasons for this:

有两个原因:

  1. You may have forgotten to close database after query execution. You need to close database after every execution.
  2. 您可能忘记在查询执行后关闭数据库。每次执行后都需要关闭数据库。

  3. You may be executing multiple queries simultaneously. In case you execute multiple queries simultaneously, you must put some delay between multiple query executions.
  4. 您可能同时执行多个查询。如果同时执行多个查询,则必须在多个查询执行之间放置一些延迟。