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

问题描述:

我很困惑为什么SELECT语句无法正常工作。它不会给我任何错误,只是返回null。我知道它正确地写入字符串,正确的字符串在那里,它只是没有正确读取它。据我所知,所有的东西都是正确的,因为我对许多其他类似的方法/函数使用了相同的SQLstmt“方法”。这只是没有意义,为什么它不应该工作。当使用SELECT语句时,iOS sqlite将返回NULL

- (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; 
} 

您不打电话给sqlite3_step。该声明从未执行。

+0

SQLite C/C++ [简介](http://www.sqlite.org/cintro.html)可能值得一读。 – 2011-12-27 02:56:03

+0

ty的功能就像是一种魅力,当你大部分复制粘贴的时候会发生什么 - 你不知道SQLite是如何工作的x_x可悲的是我之前也使用过step函数。 – kgaidis 2011-12-27 03:06:12

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); 
    }