我的SQLITE3插入方法有什么问题?

时间:2011-11-03 13:21:35

标签: objective-c

非常困惑......我的“DiscoveredCars”表格中有两个记录。我第一次使用它调用此方法时正确插入,最后一行显示“3”表示记录数。

然而第二次我称之为,SQL看起来很完美但由于某种原因它仍然显示“3”的大小并且似乎没有正确插入?有任何想法吗?

- (void)writeToDiscoveredCars: (Car *)car {

NSString *tempSQL = [NSString stringWithFormat:@"INSERT INTO DiscoveredCars (car_ID) VALUES (%i)", car.ID];

NSLog(@"size of discoveredis %i", [self numberRecordsForTable:@"DiscoveredRecipes"]);
NSLog(@"SQL insert is %@", tempSQL);

const char *sql = [tempSQL UTF8String];   
sqlite3_stmt *statement;

int sqlResult = sqlite3_prepare_v2(myDatabase, sql, -1, &statement, NULL);

if (sqlResult == SQLITE_OK) {

    sqlResult = sqlite3_step(statement);

    if(sqlResult == SQLITE_DONE)
    {

    }

    sqlite3_finalize(statement);
}

else
{
    NSLog(@"1. problem with database");
    NSLog(@"%s", sqlite3_errmsg(myDatabase));
}

NSLog(@"size of discoveredis %i", [self numberRecordsForTable:@"DiscoveredCars"]);

}

1 个答案:

答案 0 :(得分:2)

您永远不会通过致电sqlite3_step来执行您的查询。

int sqlResult = sqlite3_prepare_v2(myDatabase, sql, -1, &statement, NULL);

if (sqlResult == SQLITE_OK) {
    sqlResult = sqlite3_step(statement); //Execute!
    //check the result for completion
    if(sqlResult == SQLITE_DONE)
    {
    }

    sqlite3_finalize(statement);
}