当绑定发生在另一个函数中时,Sqlite 找不到行 _only_ [英] Sqlite doesn't find row _only_ when binding takes places in another function

查看:21
本文介绍了当绑定发生在另一个函数中时,Sqlite 找不到行 _only_的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我自己写了一个小包装函数来为我做一个准备好的声明:

So I wrote myself a little wrapper function to make a prepared statement for me:

sqlite3_stmt* Gladiateur::run_query_unfinalized(string query, vector<string> inputs){

    sqlite3_stmt *statement;

    // Prepare SQL
    sqlite3_prepare_v2(db, query.c_str(), -1, &statement, NULL);

    // Bind parameter
    sqlite3_bind_text(statement, 1, inputs[0].c_str(), -1, NULL);

    return statement;

}

通常它会遍历输入向量并绑定所有字符串,但我简化了代码以找出错误所在.

Normally it would go through the inputs vector and bind all of the strings, but I simplified the code to find out where the error lies.

我有另一个函数 set_list(也简化了):

I have another function set_list (also simplified):

int Gladiateur::set_list (string list_name) {

    vector<string> bind = {list_name};
    sqlite3_stmt *statement = this->run_query_unfinalized("SELECT id FROM lists WHERE name = ? LIMIT 1", bind);

    printf("code %d\n", sqlite3_step(statement));

    sqlite3_finalize(statement);

    return 1;

}

我调用 set_list,我得到代码 101",这意味着没有 SQL 错误但没有检索到任何行.不过,我知道存在匹配的行.

I call set_list, and I get "code 101", which simply implies that there were no SQL-errors but no row was retrieved. I know that a matching row exists, though.

奇怪的部分来了:我移动线

And here comes the weird part: I move the line

sqlite3_bind_text(statement, 1, input[0].c_str(), -1, NULL);

进入set_list函数,就在printf的上面,写bind[0]而不是inputs[0].它有效!我得到代码 100",找到了该行,当我检查时它也给了我正确的 ID.

into the set_list function, just above the printf, and write bind[0] instead of inputs[0]. And it works! I get "code 100", the row is found, it also gave me the correct id when I checked.

但我想在 run_query_unfinalized 中完成所有的绑定......我真的很困惑为什么它不起作用.也许我不能像那样传递 statement 变量?

But I want to do all the binding in run_query_unfinalized... and I am really confused why it doesn't work. Maybe I can't pass the statement variable like that?

推荐答案

sqlite3_bind_text 的最后一个参数 函数不能为 NULL:

The last parameter of the sqlite3_bind_text function cannot be NULL:

BLOB 和字符串绑定接口的第五个参数是一个析构函数,用于在 SQLite 完成后处理 BLOB 或字符串.即使调用绑定 API 失败,也会调用析构函数来处理 BLOB 或字符串.如果第五个参数是特殊值 SQLITE_STATIC,那么 SQLite 假定信息是静态的,非托管空间,不需要释放.如果第五个参数的值为 SQLITE_TRANSIENT,那么 SQLite 会制作自己的数据私有副本立即,在 sqlite3_bind_*() 例程返回之前.

The fifth argument to the BLOB and string binding interfaces is a destructor used to dispose of the BLOB or string after SQLite has finished with it. The destructor is called to dispose of the BLOB or string even if the call to bind API fails. If the fifth argument is the special value SQLITE_STATIC, then SQLite assumes that the information is in static, unmanaged space and does not need to be freed. If the fifth argument has the value SQLITE_TRANSIENT, then SQLite makes its own private copy of the data immediately, before the sqlite3_bind_*() routine returns.

在这种情况下,您需要 SQLITE_TRANSIENT.

In this case, you need SQLITE_TRANSIENT.

这篇关于当绑定发生在另一个函数中时,Sqlite 找不到行 _only_的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆