使用SELECT语句时,iOS sqlite将返回NULL

时间:2011-12-27 02:42:10

标签: iphone ios xcode sqlite select

我很困惑为什么SELECT语句无法正常工作。它没有给我任何错误,只返回null。我知道它正在正确地写字符串并且正确的字符串在那里,它只是没有正确读取它。据我所知,一切都是正确的,因为我对与此类似的许多其他方法/函数使用相同的SQLstmt“方法”。对于为什么它不起作用,这个没有意义。

- (NSString *)returnNote {
    selStmt=nil;

    NSLog(@"Reading note");

    NSString *SQLstmt = [NSString stringWithFormat:@"SELECT 'Notes' FROM '%@' WHERE Exercises = '%@';", currentRoutine, currentExercise];

    // Build select statements
    const char *sql = [SQLstmt UTF8String];

    if (sqlite3_prepare_v2(database, sql, -1, &selStmt, NULL) != SQLITE_OK) {
        selStmt = nil;
    }

    // Building select statement failed
    if (!selStmt) {
        NSAssert1(0, @"Can't build SQL to read Exercises [%s]", sqlite3_errmsg(database));
    }

    NSString *note = [NSString stringWithFormat:@"%s", sqlite3_column_text(selStmt, 0)];

    sqlite3_reset(selStmt); // reset (unbind) statement

    return note;
}

2 个答案:

答案 0 :(得分:1)

你没有打电话给sqlite3_step。声明永远不会被执行。

答案 1 :(得分:0)

    NSString *querySQLS1 = [NSString stringWithFormat: @"SELECT Notes FROM \"%@\" where Exercises=\"%@\"", currentRoutine, currentExercise];
    sqlite3_stmt *statements;
    const char *query_stmts1 = [querySQLS1 UTF8String];

    if(sqlite3_prepare_v2(UsersDB, query_stmts1, -1, &statement, NULL) == SQLITE_OK)
    {
        NSLog(@"in prepare");
        if (sqlite3_step(statement) == SQLITE_ROW)
        {
            NSLog(@"Query executed");

        } 
        else {
            NSLog(@"in else");
        }

        sqlite3_finalize(statement);
    }