Node-DuckDB API > Connection > executeIterator
Asynchronously executes the query and returns an iterator that points to the first result in the result set.
Signature:
executeIterator<T>(command: string, options?: IExecuteOptions): Promise<ResultIterator<T>>;
Parameter | Type | Description |
---|---|---|
command | string | SQL command to execute |
options | IExecuteOptions | optional options object of type IExecuteOptions |
Returns:
Promise<ResultIterator<T>>
Printing rows:
import { Connection, DuckDB, RowResultFormat } from "node-duckdb";
async function queryDatabaseWithIterator() {
const db = new DuckDB();
const connection = new Connection(db);
await connection.executeIterator("CREATE TABLE people(id INTEGER, name VARCHAR);");
await connection.executeIterator("INSERT INTO people VALUES (1, 'Mark'), (2, 'Hannes'), (3, 'Bob');");
const result = await connection.executeIterator("SELECT * FROM people;");
// print the first row
console.log(result.fetchRow());
// print the rest of the rows
console.log(result.fetchAllRows());
const result2 = await connection.executeIterator("SELECT * FROM people;", { rowResultFormat: RowResultFormat.Array });
console.log(result2.fetchAllRows());
connection.close();
db.close();
}
queryDatabaseWithIterator();
Providing generics type:
const result = await connection.executeIterator<number[]>(`SELECT CAST(1 AS TINYINT)`, {
rowResultFormat: RowResultFormat.Array,
});
expect(result.fetchRow()).toMatchObject([1]);