开发者

Debugging an SQLite query from a C program

I have an SQL table where one of the columns has several comma separated values. The code below is supposed to go through all of them and return true if a particular value is present in the list, however it seems to only work with values other than the first value in the entry. Any ideas about where I screwed up? The query works fine when running it directly on the database file using sqlite3, so I'm sure the problem is with this function.

bool group_exists(char *group)
{
    int retv;
    char *param_1, *param_2;
    bool exists = false;
    sqlite3_stmt *p_stmn;

    param_1 = malloc(BUFFER_SIZE);
    param_2 = malloc(BUFFER_SIZE);

    sprintf(param_1, "%s,%%", group);
    sprintf(param_2, "%%,%s,%%", group);

    sqlite3_prepare_v2(db, "SELECT groups FROM users WHERE groups LIKE ? OR groups LIKE ?", -1, &p_stmn, NULL);
    sqlite3_bind_text(p_stmn, 1, param_1, -1, NULL);
    sqlite3_bind_text(p_stmn, 1, param_2, -1, NULL);
    retv = sqlite3_step(p_stmn);

    if (retv == SQLITE_ROW) {
        exists = true;
    } else if 开发者_如何学JAVA(retv != SQLITE_DONE) {
        retval_crash();
    }

    free(param_1);
    free(param_2);
    sqlite3_finalize(p_stmn);

    return exists;
}


Make sure you bind the indexes to the correct sql parameter.

sqlite3_bind_text(p_stmn, 1, param_1, -1, NULL);
sqlite3_bind_text(p_stmn, 2, param_2, -1, NULL);

Also when trying to debug sqlite be sure to check the result codes to make sure everything is successful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜