sqlite3_exec不返回任何输出

时间:2017-10-21 22:31:36

标签: c sqlite

我的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");
}

但是永远不会执行。

sqlite3_exec()返回SQLITE_OK即使我出现区分大小写错误。

1 个答案:

答案 0 :(得分:0)

好吧,我在没有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;
}