在ios中更新sqlite行时出错

时间:2015-06-23 13:40:28

标签: ios sql sqlite sql-update

我在更新行时遇到错误"错误#34;我试图在iOS中更新sqlite行时出错。这是我的更新方法:

-(BOOL) updateResultForLesson:(int)lessonId result:(int)result{
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {

        sqlite3_stmt *statement;
        NSString *querySQL = [NSString stringWithFormat:@"update lessons set result=%d where id=%d",result, lessonId];

        const char *query_stmt = [querySQL UTF8String];

        sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL);


        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            NSLog(@"updated");
        return YES;
        }
        else {
            sqlite3_reset(statement);
            NSLog(@"error while updating row");
            return NO;
        }
    }
    return NO;

}

这里插入方法工作正常:

-(BOOL) addLesson:(NSString *)title levelId:(NSInteger)levelId categoryId:(NSInteger)categoryId{
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        int result = 0;

        NSString *insertSQL = [NSString stringWithFormat:@"insert into lessons (title, level_id, category_id, result) values(\"%@\", \"%d\", \"%d\", \"%d\")",title, levelId, categoryId, result];

        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);

        if (sqlite3_step(statement) == SQLITE_DONE)
        {
           ;
            return YES;
        }
        else {
            sqlite3_reset(statement);
            return NO;
        }
    }
    return NO;

}
我做错了什么?有人可以帮忙吗?插入和选择方法效果很好,我只有更新行的问题。

2 个答案:

答案 0 :(得分:2)

我建议你使用sqlite3_bind_ functions:

SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);

并检查sqlite3_prepare_v2是否返回'OK'结果。

试试这段代码:

-(BOOL) updateResultForLesson:(int)lessonId result:(int)result{
    const char *dbpath = [databasePath UTF8String];
     BOOL success = NO;
    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {

        sqlite3_stmt *statement;

        const char *query_stmt = "update lessons set result= ? where id= ? ";

        if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) ==SQLITE_OK)
        {

            int bindRes = 0;
            bindRes = sqlite3_bind_int64(statement, 0, result);
            bindRes = sqlite3_bind_int64(statement, 1, lessonId);


            int state = sqlite3_step(statement);
            if (state == SQLITE_DONE)
            {
                success = YES;
            }
            else {
                NSLog(@"Failed to update record");
                success = NO;
            }
        }


        sqlite3_reset(statement);
        sqlite3_finalize(statement);

        return success;
}

警告: 我不知道'result'的类型,所以你必须对列表中的语句使用正确的bind函数

答案 1 :(得分:0)

更改您的更新查询,您错过了查询中的值的引用

NSString *querySQL = [NSString stringWithFormat:@"update lessons set result=\"%d\" where id=\"%d\"",result, lessonId];