“未知错误”在 iPhone 上的 SQLite 中选择记录总数时

发布于 2024-09-08 09:02:00 字数 924 浏览 4 评论 0原文

尝试返回 iPhone 上的 SQLite 中是否存在记录,但我不断收到“未知错误”。

selectStmt 是 static sqlite3_stmt *selectStmt = nil;,这里使用 if(selectStmt) sqlite3_finalize(selectStmt);,它仅在应用程序终止时执行。此功能适用于删除语句和插入语句,所以我猜下面的逻辑有问题?

- (BOOL) doesBookExist {

    if(selectStmt == nil) {
        const char *sql = "select count(*) from books where isbn = ?";

        if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating select statement. '%s'", sqlite3_errmsg(database));
    }

    //When binding parameters, index starts from 1 and not zero.
    int count = sqlite3_bind_text(selectStmt, 1, [isbn UTF8String], -1, SQLITE_TRANSIENT);

    if (SQLITE_DONE != sqlite3_step(selectStmt)) 
        NSAssert1(0, @"Error while selecting. '%s'", sqlite3_errmsg(database));

    sqlite3_reset(selectStmt);

    return (count > 0);
}

Trying to return whether a record exists in SQLite on the iPhone except I keep getting an 'unknown error'.

selectStmt is static sqlite3_stmt *selectStmt = nil; used here if(selectStmt) sqlite3_finalize(selectStmt); which only gets executed if the application terminates. This functionality works fine with delete statements and insert statements so I'm guessing it's something wrong with the below logic?

- (BOOL) doesBookExist {

    if(selectStmt == nil) {
        const char *sql = "select count(*) from books where isbn = ?";

        if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating select statement. '%s'", sqlite3_errmsg(database));
    }

    //When binding parameters, index starts from 1 and not zero.
    int count = sqlite3_bind_text(selectStmt, 1, [isbn UTF8String], -1, SQLITE_TRANSIENT);

    if (SQLITE_DONE != sqlite3_step(selectStmt)) 
        NSAssert1(0, @"Error while selecting. '%s'", sqlite3_errmsg(database));

    sqlite3_reset(selectStmt);

    return (count > 0);
}

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

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

发布评论

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

评论(1

江湖彼岸 2024-09-15 09:02:00

sqlite3_bind_text 返回成功/错误代码,而不是结果任何查询。并且步骤应该返回 SQLITE_ROW,因为您有一行结果数据(无论计数是 0 还是更多)。似乎存在错误,因为您期望 SQLITE_DONE,而正确的值为 SQLITE_ROW。然后,要获取计数,您需要使用 sqlite3_column_int 执行步骤后。所以像这样:

int bind_res = sqlite3_bind_text(selectStmt, 1, [isbn UTF8String], -1, SQLITE_TRANSIENT);

if (SQLITE_OK != bind_res)
{
  // log error, return...
}

// One row of result data, so step should return SQLITE_ROW
if (SQLITE_ROW != sqlite3_step(selectStmt)) 
{
  NSAssert1(0, @"Error while selecting. '%s'", sqlite3_errmsg(database));
  // log error, return
}

int count = sqlite3_column_int(selectStmt, 0); 

sqlite3_bind_text returns a success/error code, not the result of any query. And step should return SQLITE_ROW, since you have one row of result data (regardless of whether the count is 0 or more). There seemed to be an error, because you were expecting SQLITE_DONE when the correct value was SQLITE_ROW. Then, to get the count, you need to use sqlite3_column_int after executing step. So something like:

int bind_res = sqlite3_bind_text(selectStmt, 1, [isbn UTF8String], -1, SQLITE_TRANSIENT);

if (SQLITE_OK != bind_res)
{
  // log error, return...
}

// One row of result data, so step should return SQLITE_ROW
if (SQLITE_ROW != sqlite3_step(selectStmt)) 
{
  NSAssert1(0, @"Error while selecting. '%s'", sqlite3_errmsg(database));
  // log error, return
}

int count = sqlite3_column_int(selectStmt, 0); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文