Getting int values from SQLite

后端 未结 2 1767
爱一瞬间的悲伤
爱一瞬间的悲伤 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);
    }
    
    0 讨论(0)
  • 2021-01-16 07:06

    sqlite3_column_int(result, columnNum); will return one column from the current row of your result as an int.

    Your prepare function is to prepare your query, it has nothing to do with how the results are interpreted. All data in sqlite3 is stored textually, you use the appropriate function to retrieve a value in the type you believe it should be.

    0 讨论(0)
提交回复
热议问题