简体   繁体   English

在Objective-c中从PhoneBook插入一些联系人后,数据库无法打开

[英]Database could not open after insert some contact from PhoneBook in Objective-c

Database Could not able to open after insert few record from iPhone Contact and application UI irresponsive and crash 从iPhone联系人和应用程序用户界面插入几条记录后,数据库无法打开,但无响应并崩溃

-(BOOL)InsertUserBirtdayInfo:(UserBirthDayInfo*) objBirthDayInfo
{
isOperationCompleted=NO;
@try
{
    if ( sqlite3_open_v2([databasePath UTF8String], &calledDB, SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK)
    {
        statementInsert = nil;

        NSString *insert=[NSString stringWithFormat:@"INSERT INTO %@ (%@, %@ ,%@, %@ ,%@ ,%@ , %@ , %@ ,%@ ,%@ ,%@, %@,%@) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)",_Table,_Name,_Phone,_UserImage,_Birthday_Day,_Birthday_Month,_Birthday_Year,_isDisplayOnCalender,_isMessageDefault,_Default_Message,_Custom_Message,_isAutoPost,_isTextMessage,_isFacebook];
        const char *qryInsert = [insert UTF8String];
        if (sqlite3_prepare_v2(calledDB, qryInsert, -1, &statementInsert, NULL) != SQLITE_OK)
        {
            NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(calledDB));
        }else{

            sqlite3_bind_text(statementInsert, 1, [objBirthDayInfo.Name UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statementInsert, 2, [objBirthDayInfo.Phone UTF8String], -1, SQLITE_TRANSIENT);

            NSData *dataForImage = objBirthDayInfo.User_Image;
            sqlite3_bind_blob(statementInsert,3, [dataForImage bytes],(int) [dataForImage length], SQLITE_TRANSIENT);

            sqlite3_bind_int(statementInsert,4, objBirthDayInfo.BirthDay_Day.intValue);
            sqlite3_bind_int(statementInsert,5, objBirthDayInfo.BirthDay_Month.intValue);
            sqlite3_bind_int(statementInsert,6, objBirthDayInfo.BirthDay_Year.intValue);

            sqlite3_bind_int(statementInsert,7,(int) objBirthDayInfo.isDisplayCalender);
            sqlite3_bind_int(statementInsert,8,(int)objBirthDayInfo.isMessageDefault);

            sqlite3_bind_text(statementInsert,9, [objBirthDayInfo.defaultMessage UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statementInsert,10, [objBirthDayInfo.customMessage UTF8String], -1, SQLITE_TRANSIENT);
           // sqlite3_bind_int(statementInsert,11,(int) objBirthDayInfo.isNotification);
            sqlite3_bind_int(statementInsert, 12,(int) objBirthDayInfo.isAutoPost);
            sqlite3_bind_int(statementInsert, 13,(int) objBirthDayInfo.isTextMessage);
            sqlite3_bind_int(statementInsert, 14,(int) objBirthDayInfo.isFacebook);

            if(sqlite3_step(statementInsert)==SQLITE_DONE)
            {
                isOperationCompleted=YES;
                NSLog(@"User birthdate information inserted successfully on database!");

            }

            else
            {

                NSLog(@"Failed to Insert User birthdate information!");
            }

        }

    }

    else
    {
        NSLog(@"Insert Record");

        NSLog(@"Failed to open database with error %s", sqlite3_errmsg(calledDB));
    }

}


@catch (NSException *exception)
{
    isOperationCompleted=NO;
    NSLog(@"%@",exception);
}
@finally
{
   sqlite3_reset(statementInsert);
    sqlite3_close(calledDB);
}
return isOperationCompleted;
}

You shouldn't just skip column 11. If you don't want to bind a value to it, use sqlite3_bind_null . 您不应该只跳过第11列。如果不想将值绑定到它,请使用sqlite3_bind_null But given that you only have 13 columns and you're binding using indexes up to 14, I'm presuming you removed isNotification , so perhaps you want to renumber indexes 12-14 to be 11-13? 但是鉴于您只有13列,并且要使用不超过14的索引进行绑定,我假设您已删除isNotification ,所以也许您想将索引12-14重新编号为11-13? Bottom line, check the result of the sqlite3_bind_xxx calls and it would have caught this, methinks. 最起码,请检查sqlite3_bind_xxx调用的结果,方法将被捕获。

Also, btw, do not call sqlite3_reset . 另外,顺便说一句,不要调用sqlite3_reset (That's for resetting a prepared statement so that you can bind new values in order to call sqlite3_step again.) You should instead use sqlite3_finalize . (这是为了重置准备好的语句,以便您可以绑定新值以便再次调用sqlite3_step 。)您应该改用sqlite3_finalize

If you're still having problems, tell us what the exception was. 如果您仍然遇到问题,请告诉我们什么是例外。 Or if you're not going to share that with us, refer to My App Crashed, Now What? 或者,如果您不想与我们分享,请参阅“ 我的应用崩溃了,现在如何?” . And I understand why you may have added exception handling, but that's not a preferred error handling mechanism in Objective-C and I'd remove it so that you an see what the full stack trace and full details of the exception in the debugger. 而且我知道为什么您可能添加了异常处理,但是在Objective-C中这不是首选的错误处理机制,因此我将其删除,以便您在调试器中看到异常的完整堆栈跟踪和完整详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM