On this page

Choose a query command

Choose the command by what the caller needs to receive. A statement that returns rows needs a row-returning command; a write without rows usually needs exec.

Match the command to the result

Caller needsCommandResult
One rowoneA row and an error; no match returns the driver's no-rows error.
All matching rowsmanyA slice and an error.
Success or failure of a writeexecAn error.
Affected-row countexecrowsA count and an error.
The driver's execution resultexecresultThe result and an error.

Put the name and command in the query header. This example reads one boat:

-- sgl.query(GetBoat, one)
SELECT id, name, berth, at_sea FROM boats WHERE id = ?;

Run sgl vet and sgl generate, then call the generated method:

boat, err := queries.GetBoat(ctx, id)

Check the error before using boat. Handle the no-rows error as a missing boat, or use optional single-row results when the caller should receive a presence flag instead.

Choose a different execution pattern when needed

Use stream to process rows without buffering them, or batch and copyfrom to send a slice of inputs. For a queued ClickHouse mutation, use asyncexec and account for its completion semantics.

Do not depend on LastInsertId across drivers. pgx returns a CommandTag for execresult; ClickHouse refuses both execrows and execresult because its driver reports no affected-row count (Q059). The query commands reference lists each signature and emitter restriction.

The working example includes connection setup and tests. From the repository root, run:

cd examples/sqlite/002_commands
go test .

Examples for this page