【问题标题】:sqlite3_exec return no outputsqlite3_exec 不返回任何输出
【发布时间】:2017-10-26 07:58:17
【问题描述】:

我的 sqlite 函数看起来像这样。

static int callback(void *not_used, int row_num, char **row, char **col_name)
{
    for(int i = 0; i < row_num; ++i)
    {
        printf("%s = %s\n", col_name[i], row[i] ? row[i] : "NULL");
    }
    return 0;
}

int query(const char *statement)
{
    result = sqlite3_exec(database, statement, callback, 0, &message);
    if(result != SQLITE_OK)
    {
        print_error(sqlite3_errmsg(database));
    }
    return result;
}

但我的查询不显示任何值。 sql查询有区分大小写错误

"select * from app_list where name='evince'" // it should be Evince

如果有人更改我的代码以返回一个空集 犯了这样的错误(我应该修改回调或查询哪个部分)

我尝试改变回调

if(row_num >0)
{
    for() ...
}
else
{
    printf("Empty set\n");
}

但永远不会执行 else。

即使我犯了区分大小写错误,sqlite3_exec() 也会返回 SQLITE_OK。

【问题讨论】:

  • 计算返回的行数。事后检查。使用光标界面会更好。

标签: c sqlite


【解决方案1】:

好吧,我在没有 sqlite3_exec 和回调的情况下解决了我的问题

int query(const char *statement)
{
    sqlite3_stmt *stmt = NULL;
    result = sqlite3_prepare_v2(database, statement, -1, &stmt, NULL);
    if(result != SQLITE_OK)
    {
        print_error(sqlite3_errmsg(database));
        return result;
    }

    int row_count = 0;
    result = sqlite3_step(stmt);
    while(result == SQLITE_ROW)
    {
        row_count++;
        printf("Line %d\n", row_count);
        int col_count = sqlite3_column_count(stmt);
        for (int col_i = 0; col_i < col_count; ++col_i)
        {
            int type = sqlite3_column_type(stmt, col_i);
            printf("%s\t", sqlite3_column_name(stmt, col_i));
            if (type == SQLITE_INTEGER)
            {
                printf("%d\n", sqlite3_column_int(stmt, col_i));
            }
            else if (type == SQLITE_FLOAT)
            {
                printf("%f\n", sqlite3_column_double(stmt, col_i));
            }
            else if (type == SQLITE_TEXT)
            {
                printf("%s", sqlite3_column_text(stmt, col_i));
            }
            else if (type == SQLITE_BLOB)
            {
                printf("BLOB\n");
            }
            else if (type == SQLITE_NULL)
            {
                printf("NULL\n");
            }
        }
        result = sqlite3_step(stmt);
    }

    if(row_count == 0)
    {
        printf("Empty set.\n");
    }

    result = sqlite3_finalize(stmt);
    return result;
}

【讨论】:

    猜你喜欢
    • 2014-09-09
    • 1970-01-01
    • 2012-02-04
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2015-07-27
    相关资源
    最近更新 更多