所有后续的插入语句总是在目标C的sqlite3中输入第一个插入语句

时间:2013-11-17 22:37:18

标签: objective-c insert sqlite

所有后续INSERT语句总是在目标C

中的sqlite3中输入第一个插入语句

例如,如果,当表为空时,我输入'label = x'和'score = y',所有其他后续INSERT语句为'label'和'score'插入相同的值。

以下是我的插入方法的代码:

- (void) insertDataInDatabase:(NSString*)label arg2:(double)score{

// setting up path to db file
sqlite3_stmt *insert_statement = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,       NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"MyDB.sqlite"];

// actually opening db and executing insert statement
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {

    if (insert_statement == nil) {
    static char *sql = "";
    NSString *strSQL = [NSString stringWithFormat:@"INSERT INTO scores (label, score) VALUES('%@',%f)", label, score];
    sql = [strSQL UTF8String];
    sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL);           
    }

    if (sqlite3_step(insert_statement) == SQLITE_DONE)
    {
        NSLog(@"INSEEEERT!!!");
    }
    else
    {
        NSLog(@"failed");
    }

}
sqlite3_clear_bindings(insert_statement);
sqlite3_reset(insert_statement);
sqlite3_finalize(insert_statement);
sqlite3_close(database);
}

请帮帮忙?!

0 个答案:

没有答案