SQLite'更新时出错。 '约束失败''

时间:2012-01-15 13:05:29

标签: iphone ios xcode sqlite

尝试了8个小时,不太确定哪个部分出了问题。我知道是ID问题,因为我尝试了最后一条记录。它应该是ID 5,但NSlog显示6。 我删除了所有唯一ID,因此不是保留密钥问题。

感谢您的光临。

请帮忙,我发疯了........

- (id) initWithPrimaryKey:(NSInteger)pk
{   
    self = [super init];

    ID = pk;
    isDetailViewHydrated = NO;

    return self;

}

- (void)saveAllData
{
    if(isDirty) {

        if(updateStmt == nil) {

            const char *sql = "UPDATE identifyF SET TypeName = ?, gDate = ?, Location = ?, GeoCode = ?,Note = ?, Image = ?, TypeID = ? WHERE ID = ?";

            if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
                NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));
        }
        sqlite3_bind_text(addStmt, 1, [TypeName UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(updateStmt, 2, [DateStr UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(updateStmt, 3, [Location UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(updateStmt, 4, [GeoCode UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(updateStmt, 5, [Note UTF8String], -1, SQLITE_TRANSIENT);

        //image
        NSData *imgData = UIImagePNGRepresentation(self.Image);
        int returnValue = -1;
        if(self.Image != nil)
            returnValue = sqlite3_bind_blob(updateStmt, 6, [imgData bytes], [imgData length], NULL);
        else
            returnValue = sqlite3_bind_blob(updateStmt, 6, nil, -1, NULL);

        sqlite3_bind_text(updateStmt, 7, [TypeID UTF8String], -1, SQLITE_TRANSIENT);

        sqlite3_bind_int(updateStmt, 8, ID);;

        NSLog(@"frog name %@",TypeName);
        NSLog(@"frog date %@",DateStr);
        NSLog(@"location %@",Location);
        NSLog(@"Geo code %@",GeoCode);
        NSLog(@"note %@",Note);
        NSLog(@"Img %@",Image);
        NSLog(@"Type ID %@",TypeID);
        NSLog(@"Frog ID %d",ID);


        if(returnValue != SQLITE_OK)
            NSLog(@"Not OK");

        if(SQLITE_DONE != sqlite3_step(updateStmt))
            NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(database));

        sqlite3_reset(updateStmt);

        isDirty = NO;
    }

    isDetailViewHydrated = NO;
}









+ (void) getInitialDataToDisplay:(NSString *)dbPath
{   
    FinderAppDelegate * appDelegate = (FinderAppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.idArray removeAllObjects];
    //identifyArray = [[NSMutableArray alloc] init]; 
    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

        const char *sql = "SELECT ID,TypeID,TypeName,Date,Location,GeoCode,Note,Image FROM identify";
        sqlite3_stmt *selectstmt;
        if (sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

            while(sqlite3_step(selectstmt) == SQLITE_ROW) 
            {
                NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
                identify *my = [[identify alloc] initWithPrimaryKey:primaryKey];
               // my.TypeID = sqlite3_column_int(selectstmt, 1);
                my.TypeID = [NSString stringWithUTF8String:(char *) sqlite3_column_text(selectstmt,1)];
                my.TypeName = [NSString stringWithUTF8String:(char *) sqlite3_column_text(selectstmt,2)];

                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];        
                NSString *dateStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
                my.Date = [formatter dateFromString:dateStr];//[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];

                my.Location = [NSString stringWithUTF8String:(char *) sqlite3_column_text(selectstmt, 4)];
                my.GeoCode = [NSString stringWithUTF8String:(char *) sqlite3_column_text(selectstmt, 5)];
                my.Note = [NSString stringWithUTF8String:(char *) sqlite3_column_text(selectstmt, 6)];
                const char *raw = sqlite3_column_blob(selectstmt, 7);
                int rawLen = sqlite3_column_bytes(selectstmt, 7);
                NSData *data = [NSData dataWithBytes:raw length:rawLen];
                my.Image = [[UIImage alloc] initWithData:data];

                my.isDirty = NO;

                [appDelegate.idArray addObject:my];
            }                   
        }
    } else sqlite3_close(database); //close db to release all memory
}


*** Assertion failure in -[identify saveAllData], /Users/Desmond/Desktop/Finder desmond new design 2/Finder/identify.m:241
Current language:  auto; currently objective-c

Catchpoint 2 (exception thrown).objc[58781]: EXCEPTIONS: throwing 0x130db720 (object 0x1418bab0, a NSException)
objc[58781]: EXCEPTIONS: searching through frame [ip=0x1cb3db3 sp=0xbffff4c0] for exception 0x130db700
objc[58781]: EXCEPTIONS: unwinding through frame [ip=0x1cb3db3 sp=0xbffff4c0] for exception 0x130db700
objc[58781]: EXCEPTIONS: handling exception 0x130db700 at 0x1cb3e46
objc[58781]: EXCEPTIONS: rethrowing current exception
objc[58781]: EXCEPTIONS: searching through frame [ip=0x1cb3e0f sp=0xbffff4c0] for exception 0x130db700
objc[58781]: EXCEPTIONS: terminating
objc[58781]: EXCEPTIONS: searching through frame [ip=0x1f0ef21 sp=0xbffff450] for exception 0x130db700
objc[58781]: EXCEPTIONS: catch(id)
objc[58781]: EXCEPTIONS: unwinding through frame [ip=0x1f0ef21 sp=0xbffff450] for exception 0x130db700
objc[58781]: EXCEPTIONS: handling exception 0x130db700 at 0x1f0ef33
2012-01-15 22:58:07.006 FrogFinder[58781:15b03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error while updating. 'constraint failed''
*** First throw call stack:
(0x1d7d052 0x1f0ed0a 0x1d25a78 0x132c2db 0x93167 0x952c9 0x1d7eec9 0x9a65c2 0xbe1d54 0x1d7eec9 0x9a65c2 0x9a655a 0xa4bb76 0xa4c03f 0xa4b2fe 0x9cba30 0x9cbc56 0x9b2384 0x9a5aa9 0x27a1fa9 0x1d511c5 0x1cb6022 0x1cb490a 0x1cb3db4 0x1cb3ccb 0x27a0879 0x27a093e 0x9a3a9b 0x1f3a 0x1ed5)

1 个答案:

答案 0 :(得分:1)

sqlite3_bind_text(addStmt, 1, [TypeName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 2, [DateStr UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 3, [Location UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 4, [GeoCode UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 5, [Note UTF8String], -1, SQLITE_TRANSIENT);

确定第一个应该是addStmt吗?

鉴于这是唯一的位置,它在显示的代码中使用,并且在一个明确设计用于填充八个updateStmt变量的部分中,我会说它很可能是一个错字或剪切和粘贴错误。