Skip to main content
Go to Latest
method PreparedQuery.prototype.first
import { PreparedQuery } from "https://deno.land/x/sqlite@v3.7.0/mod.ts";

Binds the given parameters to the query and returns the first resulting row or undefined when there are no rows returned by the query.

Examples

const query = db.prepareQuery<[number, string]>("SELECT id, name FROM people");
const person = query.first();
// person = [1, "Peter"]
const query = db.prepareQuery("SELECT id, name FROM people WHERE name = ?");
const person = query.first(["not a name"]);
// person = undefined

To avoid SQL injection, user-provided values should always be passed to the database through a query parameter.

const query = db.prepareQuery("SELECT id FROM people WHERE name = ?");
query.first([name]);
const query = db.prepareQuery("SELECT id FROM people WHERE name = :name");
query.first({ name });

See QueryParameterSet for documentation on how values can be bound to SQL statements.

See QueryParameter for documentation on how values are returned from the database.

Parameters

optional
params: P

Returns

R | undefined