Sqlite 数据库在插入数据时在某些情况下被锁定 [英] Sqlite Database locked in some case while inserting data

查看:23
本文介绍了Sqlite 数据库在插入数据时在某些情况下被锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Iphone 应用程序有问题.有一段时间我的应用程序运行成功,但在某些情况下它会给出数据库被锁定异常",这就是为什么我无法从 sqlite 数据库读取或插入数据的原因.如果有人有解决方案,请建议我.这是我将数据插入数据库的代码谢谢.

I have a problem in my Iphone application. Some time my Application run successfully,But in some cases it give "databse is locked exception"thats why i am unable to read or insert data from sqlite datase. Please suggest me if any one have some solution. This is my code of inserting data into databse Thanks.

-(void)insertDataIntoDatabase
{   
    NSLog(@"insertDataIntoDatabase-----1");

    @try{
        tUserName=userNameTf.text;
        tLevel=[NSString stringWithFormat:@"%d",level];
        tMoves=[NSString stringWithFormat:@"%d",moves];

        NSLog(@"tLevel;;;%@",tLevel);
        //   NSString *tdatabaseName = @"FlipScoreBord.sqlite";
        sqlite3_stmt *addStatement;
        //  NSArray *tdocumentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
        //  NSString *tdocumentsDir = [tdocumentPaths objectAtIndex:0];
        // NSString *tdatabasePath = [tdocumentsDir stringByAppendingPathComponent:tdatabaseName];
        NSString *insertQuery = [NSString stringWithFormat:@"INSERT INTO Moves (User_Name,User_Label,User_Moves) VALUES('%@','%@','%@')",tUserName,tLevel,tMoves];
        const char *sql = [insertQuery cStringUsingEncoding:NSUTF8StringEncoding];

        if(sqlite3_prepare_v2(tdatabase, sql, -1, &addStatement, NULL) != SQLITE_OK)
        {
            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(tdatabase));
        }
        sqlite3_bind_text(addStatement, 0, [tUserName UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(addStatement, 1, [tLevel UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(addStatement, 2,  [tUserName UTF8String], -1, SQLITE_TRANSIENT);

        if(SQLITE_DONE != sqlite3_step(addStatement))
        {
            NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(tdatabase));
            sqlite3_reset(addStatement);
        }
        sqlite3_close(tdatabase);
    }

    @catch (NSException *r)
    {
        NSLog(@"Exception---- %@",r);
    }
    NSLog(@"insertDataIntoDatabase-----2");  

}

推荐答案

此链接列出了触发数据库锁定错误的原因:

This link lists the reasons how a database lock error can be triggered:

http://www.sqlite.org/cvstrac/wiki?p=DatabaseIsLocked

引用原因之一:

当 SELECT 在同一个表上处于活动状态时尝试写入该表.

Trying to write to a table while a SELECT is active on that same table.

由于您没有在您的语句中调用 sqlite3_finalize,有可能之前的 'SELECT' 语句阻塞了您的 'INSERT'.在调用 sqlite3_close 之前尝试添加一个 sqlite3_finalize.

Since you did not call the sqlite3_finalize on your statement, it is possible that a previous 'SELECT' statement is blocking your 'INSERT'. Try adding an sqlite3_finalize before you call the sqlite3_close.

这篇关于Sqlite 数据库在插入数据时在某些情况下被锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆