目标c中的断言失败错误

时间:2012-03-28 14:43:49

标签: iphone objective-c ios xcode sqlite

我有Json数据,但是当我尝试将此记录插入sqlite数据库时,我得到了此错误断言失败。我可以使用NSLog查看数据但我无法插入数据库。怎么了?我怎么解决?谢谢你的答复。我的代码如下:

 -(void)processDoneWithRequestName:(NSString*)tagName{

sqlite3_stmt *stmt=nil;


//NSArray *pathsArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *doumentDirectoryPath=@"/Users/ds/Desktop/SqliteTest/SqliteTest";
NSString *cruddatabase=[doumentDirectoryPath stringByAppendingPathComponent:@"SqliteTestDb.sqlite"];

if ([tagName isEqualToString:Logins]) {

    int keys = [[response.item objectForKey:@"Lipton"] count];
    NSLog(@"count %i",keys);
    for (int i=0; i<keys; i++)
    {

        NSString *str1 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"IsSuccess"];
        NSString *str2 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"ProducerId"];
        NSString *str3 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Latitude"];
        NSString *str4 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Longitude"];
        NSString *str5 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Altitude"];
        NSString *str6 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Slope"];
        NSString *str7 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"SampleDate"];
        NSString *str8 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"PackageNo"];
        NSString *str9 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Status"];
        NSString *str10 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Description"];


        if (sqlite3_open([cruddatabase UTF8String], &cruddb)==SQLITE_OK)  {


            const char *sql = "INSERT INTO LabUpdate (IsSuccess, ProducerId, Latitude, Longitude, Altitude, Slope, SampleDate, PackageNo, Status, Description) VALUES (?,?,?,?,?,?,?,?,?,?)";            

            NSInteger result = sqlite3_prepare_v2(cruddb,sql, -1, &stmt, NULL);
            if(sqlite3_step(stmt) == SQLITE_ROW || result == SQLITE_ROW) 
            {
                sqlite3_prepare_v2(cruddb, sql, 1, &stmt, NULL);
                sqlite3_bind_int(stmt, 1, [str1 integerValue]);
                sqlite3_bind_int(stmt, 2, [str2 integerValue]);
                sqlite3_bind_double(stmt, 3, [str3 floatValue]);
                sqlite3_bind_double(stmt, 4, [str4 floatValue]);
                sqlite3_bind_double(stmt, 5, [str5 floatValue]);
                sqlite3_bind_double(stmt, 6, [str6 floatValue]);
                sqlite3_bind_text(stmt, 7, [str7 UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_bind_int(stmt, 8, [str8 integerValue]);
                sqlite3_bind_int(stmt, 9, [str9 integerValue]);
                sqlite3_bind_text(stmt, 10, [str10 UTF8String], -1, SQLITE_TRANSIENT);
            }

            else
            {
                NSAssert1(0, @"Error . '%s'", sqlite3_errmsg(cruddb));                        
            }
            sqlite3_reset(stmt);
            sqlite3_finalize(stmt);
        }
    }                       
    sqlite3_close(cruddb);
}  
}

2 个答案:

答案 0 :(得分:0)

我不认为你的路径是正确的,它不应该是硬编码的,应该以file://开头。

使用它来获取文档目录并保存在那里

NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
    NSUserDomainMask, YES);

docsDir = [dirPaths objectAtIndex:0];

答案 1 :(得分:0)

查看以下代码

if (sqlite3_open([cruddatabase UTF8String], &cruddb)==SQLITE_OK)  {

sqlite3_prepare_v2(cruddatabase, "BEGIN TRANSACTION", -1, &compiledStmt, NULL);
sqlite3_step(compiledStmt);
sqlite3_finalize(compiledStmt);

const char *sql = "INSERT INTO LabUpdate (IsSuccess, ProducerId, Latitude, Longitude, Altitude, Slope, SampleDate, PackageNo, Status, Description) VALUES (?,?,?,?,?,?,?,?,?,?)";  
if(sqlite3_prepare_v2(cruddatabase, sql, -1, &stmt, NULL) == SQLITE_OK){  

    sqlite3_prepare_v2(cruddb, sql, 1, &stmt, NULL);
            sqlite3_bind_int(stmt, 1, [str1 integerValue]);
            sqlite3_bind_int(stmt, 2, [str2 integerValue]);
            sqlite3_bind_double(stmt, 3, [str3 floatValue]);
            sqlite3_bind_double(stmt, 4, [str4 floatValue]);
            sqlite3_bind_double(stmt, 5, [str5 floatValue]);
            sqlite3_bind_double(stmt, 6, [str6 floatValue]);
            sqlite3_bind_text(stmt, 7, [str7 UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_int(stmt, 8, [str8 integerValue]);
            sqlite3_bind_int(stmt, 9, [str9 integerValue]);
            sqlite3_bind_text(stmt, 10, [str10 UTF8String], -1, SQLITE_TRANSIENT);

        NSUInteger err = sqlite3_step(compiledStmt);
        if (err != SQLITE_DONE){
            NSLog(@"error while binding %d %s",err, sqlite3_errmsg(database));
        }
        sqlite3_reset(compiledStmt);
    sqlite3_finalize(compiledStmt);     
} else {
    NSLog(@"Invalid Query");
}

sqlite3_prepare_v2(cruddatabase, "END TRANSACTION", -1, &compiledStmt, NULL);
sqlite3_step(compiledStmt);
sqlite3_finalize(compiledStmt); 
sqlite3_close(cruddatabase);