【问题标题】:Database is locked -sqlite3数据库被锁定-sqlite3
【发布时间】:2013-03-06 18:57:50
【问题描述】:

-(void) dropCategories{

        if (deleteCategoryStmt == nil) {
            const char *deleteSql = "delete from tb_category";
            if (sqlite3_prepare_v2(database, deleteSql, -1, &deleteCategoryStmt, NULL) != SQLITE_OK) 
                    NSAssert1(0,@"Error in preparing drop category statement with '%s'", sqlite3_errmsg(database));
            else 
                    NSLog(@"NO error in creating drop categories statement");


        }

        if(SQLITE_DONE != sqlite3_step(deleteCategoryStmt))
            NSAssert1(0, @"Error while drop category data. '%s'", sqlite3_errmsg(database));

    sqlite3_reset(deleteCategoryStmt);  
    //sqlite3_finalize(deleteCategoryStmt);
    deleteCategoryStmt = nil;
}

调用此函数一次可以正常工作,但每当我再次调用它时,它就会终止并给出以下异常。

任何建议为什么会发生这种情况?

 *** Assertion failure in -[Category dropCategories], /Users/rsplmac/Documents/LiteApp2/Classes/Category.m:171
2010-08-17 14:01:06.648 LiteApp2[4335:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error while drop category data. 'database is locked''
*** Call stack at first throw:
(
    0   CoreFoundation                      0x02429919 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x025775de objc_exception_throw + 47
    2   CoreFoundation                      0x023e2078 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x000ce8cf -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   LiteApp2                            0x000043ce -[Category dropCategories] + 462
    5   LiteApp2                            0x00004ba3 -[RootViewController updateMe:] + 1297
    6   UIKit                               0x002d3e14 -[UIApplication sendAction:to:from:forEvent:] + 119
    7   UIKit                               0x004db14b -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 156
    8   UIKit                               0x002d3e14 -[UIApplication sendAction:to:from:forEvent:] + 119
    9   UIKit                               0x0035d6c8 -[UIControl sendAction:to:forEvent:] + 67
    10  UIKit                               0x0035fb4a -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    11  UIKit                               0x0035e6f7 -[UIControl touchesEnded:withEvent:] + 458
    12  UIKit                               0x002f72ff -[UIWindow _sendTouchesForEvent:] + 567
    13  UIKit                               0x002d91ec -[UIApplication sendEvent:] + 447
    14  UIKit                               0x002ddac4 _UIApplicationHandleEvent + 7495
    15  GraphicsServices                    0x02c15afa PurpleEventCallback + 1578
    16  CoreFoundation                      0x0240adc4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    17  CoreFoundation                      0x0236b737 __CFRunLoopDoSource1 + 215
    18  CoreFoundation                      0x023689c3 __CFRunLoopRun + 979
    19  CoreFoundation                      0x02368280 CFRunLoopRunSpecific + 208
    20  CoreFoundation                      0x023681a1 CFRunLoopRunInMode + 97
    21  GraphicsServices                    0x02c142c8 GSEventRunModal + 217
    22  GraphicsServices                    0x02c1438d GSEventRun + 115
    23  UIKit                               0x002e1b58 UIApplicationMain + 1160
    24  LiteApp2                            0x00002808 main + 102
    25  LiteApp2                            0x00002799 start + 53
)
terminate called after throwing an instance of 'NSException'

【问题讨论】:

    标签: iphone sqlite


    【解决方案1】:

    您对sqlite3_finalize 的呼叫似乎已被评论。对sqlite3_prepare_v2 的完整调用通常后跟sqlite_step,然后是sqlite3_finalize。不用sqlite3_reset,你能试试吗?

    编辑:对于后代:asker 找到了真正的原因,数据库被另一个对象锁定。上面的功能很好。

    【讨论】:

    • 我已经尝试过了,但似乎仍然无法正常工作。仍在终止应用程序。有什么方法可以强制解锁数据库或清除所有以前的锁?希望能解决我的问题。
    • 嗯...你试过sqlite3_exec吗?多合一查询执行!
    • 我终于发现问题出在我的代码上。我用 2 个不同的对象打开了数据库。最后把它整理出来
    • 谢谢,但我这里没有正确答案,你有。
    • 嗯实际上我已经选择了你的答案,因为它也是正确的。这就是正确的实施方式。问题出在我的代码上,根据问题,你是正确的。你的回答解决了我的问题,但它会解决其他人有同样问题但与我没有同样错误的问题.. ;)
    猜你喜欢
    • 2015-11-22
    • 2012-10-07
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    相关资源
    最近更新 更多