Encapsulates the execution state of a prepared SELECT statement. The result keeps the underlying statement alive via shared ownership so the cursor remains valid even if the originating query goes out of scope.

Use cases

  • Designed for forward-only consumption; use reset() sparingly because it rewinds the underlying statement and invalidates any spans.
  • Templated getters perform strict type conversion and throw descriptive exceptions when SQLite’s storage class cannot be mapped to T.
  • variant_t and tuple helpers make it easy to surface dynamic payloads in scripting layers without writing manual switch statements.

Iteration

MemberDescription
bool next_row()Advances to the next row; returns false at the end.
bool end() constReports whether next_row() consumed the final row.
void reset()Resets the cursor to the first row without rebinding parameters.
int get_changes()Mirrors sqlite3_changes(); only meaningful for INSERT/UPDATE/DELETE statements. Run a SELECT COUNT(*) ... query to count result rows.
int get_column_count()Number of columns exposed by the statement.

Example usage

auto cursor = sqlite::query{db,
    "SELECT id, json_extract(blob, '$.state') FROM jobs ORDER BY id"
}.get_result();

while (cursor->next_row()) {
    auto [id, state] = cursor->get_tuple<std::int64_t, std::string>();
    if (cursor->is_null(1)) {
        continue;
    }
    dispatch(id, state);
}

cursor->reset(); // rewind and reuse with new bindings if needed

Need zero-copy blob access? call get_binary_span() or get_variant(), do your work, and copy the bytes before the next next_row() call invalidates the view.

Column metadata

MemberDescription
type get_column_type(int idx)Returns the SQLite storage class for column idx.
std::string get_column_decltype(int idx)Declared type from the schema.
std::string get_column_name(int idx)Column name as compiled into the statement.

Templated extraction

For scalars, strings, optionals, durations, and enums, prefer the templated getters:

MemberDescription
template<typename T> T get(int idx)Converts the column into arithmetic types, enums, std::optional, std::chrono::duration/time_point, strings, or byte containers.
template<typename... Ts> std::tuple<Ts...> get_tuple(int start_column = 0)Reads consecutive columns into a tuple.
using result_type = std::shared_ptr<result>Alias used by sqlite::query.

Blob helpers remain available:

MemberDescription
size_t get_binary_size(int idx)Returns the blob size in bytes or 0 for NULL.
void get_binary(int idx, void* buf, size_t buf_size)Copies blob data into a caller-provided buffer (throws if too small).
void get_binary(int idx, std::vector<unsigned char>& vec)Resizes vec and copies the blob.
std::span<const unsigned char> get_binary_span(int idx)Returns a view over the blob memory.
bool is_null(int idx)Checks whether the column equals SQL NULL.