DuckDB’s C++ API provides a simple and efficient interface for embedding an analytical database directly into C++ applications. The API is designed around three main classes:
DuckDB - The database instance
Connection - A connection to the database for executing queries
QueryResult - Results from executed queries
The C++ API is header-only and requires only including duckdb.hpp.
// Materialized - entire result set is loaded into memoryauto result = con.Query("SELECT * FROM large_table");// Streaming - results are fetched incrementallyauto stream_result = con.SendQuery("SELECT * FROM large_table");
auto result = con.Query("SELECT name, age FROM users");// Print to consoleresult->Print();// Iterate over chunkswhile (auto chunk = result->Fetch()) { for (idx_t row = 0; row < chunk->size(); row++) { auto name = chunk->GetValue(0, row); auto age = chunk->GetValue(1, row); // Process values... }}// For MaterializedQueryResult, access individual valuesif (result->type == QueryResultType::MATERIALIZED_RESULT) { auto &materialized = result->Cast<MaterializedQueryResult>(); auto value = materialized.GetValue(0, 0); // column 0, row 0}