Getting int values from SQLite

后端 未结 2 1776
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-16 06:25

I heard of using sqlite3_prepare_v2 instead of sqlite_exec to get integers from database, but I failed to find any examples. This page wasn\'t help

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-16 06:53

    After sqlite3_prepare has succeeded, you must not forget to clean up the statement with sqlite3_finalize. To get the result records, call sqlite3_step until it does not return SQLITE_ROW. To get the values of the current result record, call the sqlite3_column_* functions:

    sqlite3_stmt *stmt;
    if (sqlite3_prepare_v2(db, "SELECT 42", -1, &stmt, NULL) != SQLITE_OK)
        ...error...
    else {
        for (;;) {
            int rc = sqlite3_step(stmt);
            if (rc == SQLITE_DONE)
                break;
            if (rc != SQLITE_ROW) {
                ...error...
                break;
            }
            printf("value: %d\n", sqlite3_column_int(stmt, 0));
        }
        sqlite3_finalize(stmt);
    }
    

提交回复
热议问题