更新查询时ios中的“数据库已锁定”错误

我使用以下代码使用sqlite更新查询.
但我得到“数据库锁定错误”.
我尝试搜索一些SO链接,并建议关闭数据库,但我再次这样做我得到相同的错误.我已经提到过我在代码中遇到错误的地方.
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath,&database) == sqlITE_OK)
{
    Nsstring *locationNo =NULL;
    Nsstring *querysql = [Nsstring stringWithFormat:@"select count(*) from code"];
    const char *query_stmt = [querysql UTF8String];

    if (sqlite3_prepare_v2(database,query_stmt,-1,&statement,NULL) == sqlITE_OK)
    {
        if (sqlite3_step(statement) == sqlITE_ROW)
        {
            locationNo = [[Nsstring alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,0)];
            int count= [locationNo intValue];
            sqlite3_close(database);
            NSLog(@"%@",locationNo);
            if(0==count)
            {
                Nsstring *insertsql = [Nsstring stringWithFormat:@"insert into favourite_code (code_id,code_1,code_2,code_3,code_4,code_5,code_6,status,record_status) VALUES (\"%d\",\"%@\",\"%@\")",1,code1,code2,code3,code4,code5,code6,@"Y",@"Y"];

                const char *insert_stmt = [insertsql UTF8String];
                sqlite3_prepare_v2(database,insert_stmt,NULL);
                if (sqlite3_step(statement) == sqlITE_DONE)
                {
                    return YES;
                }
                else {
                    return NO;
                }
                sqlite3_reset(statement);
                sqlite3_close(database);
            }
            else{


                =========================== Getting Error in the below lines =========================

                const char *sq1l = "update code SET code_1=?,code_2=?,code_3=?,code_4=?,code_5=?,code_6=? WHERE code_id=1";

                if (sqlite3_prepare_v2(database,sq1l,NULL) != sqlITE_OK)
                {
                    NSLog(@"Error: Failed to prepare statement with message '%s'.",sqlite3_errmsg(database));
                }
                else
                {
                    sqlite3_bind_text(statement,[code1 UTF8String],sqlITE_TRANSIENT);
                    sqlite3_bind_text(statement,2,3,4,5,6,sqlITE_TRANSIENT);

                }

                int success = sqlite3_step(statement);
                if (success != sqlITE_DONE)
                {
                    NSLog(@"Error: Failed to prepare statement with message '%s'.",sqlite3_errmsg(database));
                    //result = FALSE;
                }
                else
                {
                    NSLog(@"Error: Failed to prepare statement with message '%s'.",sqlite3_errmsg(database));
                    //result = TRUE;
                }

                =================================END=========================================   


            }

            sqlite3_reset(statement);
        }
        else
        {
            NSLog(@"Not found");
            locationNo=@"1";
        }
        sqlite3_reset(statement);
    }
}

解决方法

通常,如果您同时进行多个查询(或者您没有完成某些早期sql语句,或者您打开了多个线程,或者您已多次打开数据库),您将获得此信息.

这段代码使用了sqlite3_close和sqlite3_reset(以及缺少sqlite3_finalize),这可能是问题的根源.

An Introduction To The SQLite C/C++ Interface,他们指出了陈述的正确顺序:

> sqlite3_open(),打开数据库
> sqlite3_prepare(),准备一个sql语句
> sqlite3_bind(),将值绑定到?必要时占位符
> sqlite3_step(),执行sql和/或逐步执行结果
> sqlite3_column(),根据需要检索数据列
> sqlite3_finalize(),完成/关闭准备好的sql语句
> sqlite3_close(),关闭数据库

最后,您的sqlite3_open调用最终与单个sqlite3_close语句不匹配(但您的代码中间有一个无关的sqlite3_close).此外,每个sqlite3_prepare_v2必须有自己的sqlite3_finalize(如果你想重置一个准备好的语句,你只能使用sqlite3_reset,这样你就可以用新值绑定它并再次单步执行;但是当你完成所有准备工作时你还需要sqlite3_finalize声明).

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...