sqlite3_exec() Callback function clarification

前端 未结 2 836
滥情空心
滥情空心 2020-12-04 14:56

I am having trouble understanding the use of the callback function in a SQLite3 database.

I understand it is used to traverse SELECT statements with multiple records.

2条回答
  •  感情败类
    2020-12-04 15:14

    That tutorial is horrible, because it does not use anything but sqlite3_exec().

    In the general case, the only useful way to use sqlite3_exec() is to replace it with sqlite3_prepare_v2()/sqlite3_step()/sqlite3_column_*()/sqlite3_finalize() calls so that you can read the data in the same place where you actually need to handle it:

    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);
    

提交回复
热议问题