Inherits from sqlite::command, but adds a range-based iteration API via
each(). Bind values using bind() or %, then loop
over query.each() with a range-based for to consume rows through the
lightweight row_view proxy (supports column access by index or name).
Use cases
- Column-name lookups are cached per statement, so repeated
row["column"]calls remain O(1). each()is single-pass by design. Grabget_result()if you need to hold on to the cursor, rewind it, or interleave iteration with other work.- The class enforces correct usage—attempting to iterate without finishing the
previous
each()will throwdatabase_misuse_exception.
Public API
| Member | Description |
|---|---|
query(connection& con, std::string const& sql) | Compiles the SELECT statement. |
query(query const&)=delete, query& operator=(query const&)=delete | Non-copyable. |
~query() | Inherits command's cleanup. |
result_range each() | Returns a single-pass range whose iterators yield row_view proxies for use in range-based for loops. Each proxy exposes get<T>() by index or name. |
result_type get_result() | Constructs a manual sqlite::result cursor for low-level access. |
result_type emit_result() (deprecated) | Legacy helper that executed the statement once; prefer each() or get_result(). |
result_range exposes an iterator type that models an
input-iterator. Dereferencing yields a row_view proxy, letting you call
get<T>() inside the loop.
Example usage
Stream rows with each(), optionally passing bind arguments inline, or grab
get_result() when you need to pause iteration, rewind, or share the cursor
with another consumer.
sqlite::query q(db, "SELECT id, text FROM notes WHERE project = ?");
for (auto & row : q.each("docs")) {
auto id = row.get<std::int64_t>("id");
auto text = row.get<std::string_view>(1);
fmt::print(\"[#{}] {}\\n\", id, text);
}
sqlite::query filtered(db, "SELECT message FROM audit WHERE level = ? AND tag = ?");
filtered % "warn";
for (auto & row : filtered.each("ui")) {
log(row.get<std::string>(0));
}
sqlite::query named_q(db, "SELECT body FROM notes WHERE project = :project");
for (auto & row : named_q.each(sqlite::named(":project", "docs"))) {
consume(row.get<std::string>(0));
}
auto cursor = q.get_result(); // manual control
while (cursor->next_row()) {
if (cursor->get_column_name(0) == \"id\") {
// ...
}
}