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

时间:2015-01-02 11:11:28

标签: objective-c iphone sqlite ios7 ios8

数据库无法在从iPhone联系人和应用程序UI插入少量记录后无法打开

-(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;
}

1 个答案:

答案 0 :(得分:0)

您不应该只跳过第11列。如果您不想将值绑定到它,请使用sqlite3_bind_null。但是考虑到你只有13列并且你使用最多14的索引进行绑定,我假设你删除了isNotification,所以也许你想将索引12-14重新编号为11-13?最重要的是,检查sqlite3_bind_xxx调用的结果,它会抓住这个,这是一个问题。

另外,顺便说一下,不要拨打sqlite3_reset。 (这是为了重置预准备语句,以便您可以绑定新值以再次调用sqlite3_step。)您应该使用sqlite3_finalize

如果您仍有问题,请告诉我们例外情况。或者,如果您不打算与我们分享,请参阅My App Crashed, Now What?。我理解为什么你可能已经添加了异常处理,但这不是Objective-C中的首选错误处理机制,我将其删除,以便您在调试器中查看完整堆栈跟踪和异常的完整详细信息。 / p>