sqlite3_exec()回调函数澄清

Slv*_*rfn 19 c++ sqlite callback

我无法理解在SQLite3数据库中使用回调函数.

据我所知,它用于遍历具有多个记录的SELECT语句.但我不明白它是如何做到这一点或如何使我自己有用的回调.我已经多次阅读 TutorialsPoint以尝试理解,但这不是为我做的.

当我在Visual Studio中使用他们的示例和调试来查看如何填充和遍历参数数组时,我迷路了.VS也只显示数组中的当前插槽,而不是整个数组本身.

如果您需要任何澄清,请告诉我,因为我在这里学习!

我要求有人解释如何使用回调.也许是其他人如何使用它的一些例子.只是解释这个人甚至在做什么:

static int callback(void *data, int argc, char **argv, char **azColName){
   int i;
   fprintf(stderr, "%s: ", (const char*)data);
   for(i=0; i<argc; i++){
      printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
   }
   printf("\n");
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

Sea*_*ght 47

假设你有一个非常简单的表User,看起来像这样:

?????????????????
? ID ? Name     ?
?????????????????
? 1  ? Slvrfn   ?
? 2  ? Sean     ?
? 3  ? Drew     ?
? 4  ? mah      ?
?????????????????

你这样调用sqlite3_exec(参数在文档中有详细描述):

/* Error handling omitted for brevity */
sqlite3_exec(db, "SELECT * FROM User", my_special_callback, NULL, NULL);
Run Code Online (Sandbox Code Playgroud)

SQLite将执行传递的SQL语句,并为它找到的每个结果行调用它my_special_callback.因此,使用我们的示例User表,my_special_callback将被调用4次.所以让我们创建my_special_callback:

/*
 * Arguments:
 *
 *   unused - Ignored in this case, see the documentation for sqlite3_exec
 *    count - The number of columns in the result set
 *     data - The row's data
 *  columns - The column names
 */
static int my_special_callback(void *unused, int count, char **data, char **columns)
{
    int idx;

    printf("There are %d column(s)\n", count);

    for (idx = 0; idx < count; idx++) {
        printf("The data in column \"%s\" is: %s\n", columns[idx], data[idx]);
    }

    printf("\n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

给定我们的示例表和数据,输出将如下所示:

There are 2 column(s)
The data in column "ID" is: 1
The data in column "Name" is: Slvrfn

There are 2 column(s)
The data in column "ID" is: 2
The data in column "Name" is: Sean

There are 2 column(s)
The data in column "ID" is: 3
The data in column "Name" is: Drew

There are 2 column(s)
The data in column "ID" is: 4
The data in column "Name" is: mah

现在,如何使这个有用,这是第四个参数的sqlite3_exec来源.从文档:

sqlite3_exec()的第四个参数被中继到每个回调调用的第一个参数.

因此,假设我们要运行SQL并构建所有用户名称的链接列表.我们需要做的第一件事是改变我们的调用方式sqlite3_exec:

/* Create my fictional linked list */
struct my_linked_list *head = my_linked_list_alloc();

/*
 * Pass a pointer to my list as the 4th argument to sqlite3_exec. Error
 * handling omitted for brevity
 */
sqlite3_exec(db, "SELECT * FROM User", my_special_callback, head, NULL);

/* My list is now built, I can do stuff with it... */
my_linked_list_traverse(head, /* ... Stuff ... */);
Run Code Online (Sandbox Code Playgroud)

并修改my_special_callback使用它

/*
 * Arguments:
 *
 *     list - Pointer to a linked list of names
 *    count - The number of columns in the result set
 *     data - The row's data
 *  columns - The column names
 */
static int my_special_callback(void *list, int count, char **data, char **columns)
{
    struct my_linked_list *head = list;

    /*
     * We know that the value from the Name column is in the second slot
     * of the data array.
     */
    my_linked_list_append(head, data[1]);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在,如果您要使用callback问题中包含的内容,您可以这样称呼它:

/*
 * Pass the table name as the 4th argument to sqlite3_exec. Error
 * handling omitted for brevity
 */
sqlite3_exec(db, "SELECT * FROM User", callback, "User", NULL);
Run Code Online (Sandbox Code Playgroud)

输出将是:

User: 
ID = 1
Name = Slvrfn

User: 
ID = 2
Name = Sean

... etc ...

(除了User:部分将打印到stderr而不是stdout)

希望这有助于为您解决问题.如果还有一些你不理解的东西,请告诉我.

  • @Marvin,它在[文档](https://www.sqlite.org/c3ref/exec.html)中进行了描述。简而言之,如果非零,它会使“sqlite3_exec()”返回“SQLITE_ABORT”并防止再次调用回调。 (2认同)

CL.*_*CL. 28

该教程非常糟糕,因为它不会使用任何东西sqlite3_exec().

在一般情况下,唯一有用的方法sqlite3_exec()是用sqlite3_prepare_v2()/sqlite3_step()/sqlite3_column _*()/sqlite3_finalize()调用替换它,以便您可以在实际需要处理的相同位置读取数据它:

sqlite3_stmt *stmt;
const char *sql = "SELECT ID, Name FROM User";
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
    print("error: ", sqlite3_errmsg(db));
    return;
}
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
    int id           = sqlite3_column_int (stmt, 0);
    const char *name = sqlite3_column_text(stmt, 1);
    // ...
}
if (rc != SQLITE_DONE) {
    print("error: ", sqlite3_errmsg(db));
}
sqlite3_finalize(stmt);
Run Code Online (Sandbox Code Playgroud)

  • 也许你可以发布你未经批准的答案[在我的问题上](http://stackoverflow.com/q/35141039/105539),而不是在这里,因为它没有回答作者的问题但是回答了我的问题?顺便说一句,我在我的项目中使用了上面代码的变体,它确实完美地解决了我的问题. (3认同)