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

发布于 2024-12-23 02:57:11 字数 907 浏览 0 评论 0原文

我很困惑为什么 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;
}

I'm very confused why the SELECT statement doesn't work correctly. It doesn't give me any errors, just returns null. I know it is writing the string correctly and the right string is there, it's just not reading it correctly. Everything as far as I know is correct because I use the same SQLstmt "method" for many other methods/functions similar to this. This one just doesn't make sense on why it shouldn't work.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安穩 2024-12-30 02:57:11

您没有调用 sqlite3_step。该语句永远不会被执行。

You're not calling sqlite3_step. The statement is never executed.

墨小沫ゞ 2024-12-30 02:57:11
    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);
    }
    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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文