Skip to main content
Deno 2 is finally here 🎉️
Learn more
Go to Latest
method DB.prototype.prepareQuery
import { DB } from "https://deno.land/x/sqlite@v3.8/src/db.ts";

Prepares the given SQL query, so that it can be run multiple times and potentially with different parameters.

If a query will be issued a lot, this is more efficient than using query. A prepared query also provides more control over how the query is run, as well as access to meta-data about the issued query.

The returned PreparedQuery object must be finalized by calling its finalize method once it is no longer needed.

Typing Queries

Prepared query objects accept three type parameters to specify precise types for returned data and query parameters.

  • The first type parameter R indicates the tuple type for rows returned by the query.

  • The second type parameter O indicates the record type for rows returned as entries (mappings from column names to values).

  • The third type parameter P indicates the type this query accepts as parameters.

Note, that the correctness of those types must be guaranteed by the caller of this function.

Example

const query = db.prepareQuery<
  [string, number],
  { name: string, age: number },
  { city: string },
 >("SELECT name, age FROM people WHERE city = :city");
// use query ...
query.finalize();

Type Parameters

optional
R extends Row = Row
optional
O extends RowObject = RowObject

Parameters

sql: string