Execute raw SQL
A SQLite write or other command that must run as raw SQL.
Drizzle ORM · Module
For SQLite, db.run executes a raw parameterized SQL statement and returns the driver's run result. Use it when Drizzle's higher-level query API cannot express the required query.
For SQLite, use db.run with a parameterized SQL statement when you need to execute raw SQL that Drizzle's higher-level query APIs do not currently cover. The method returns the driver's run result rather than rows; use db.all, db.get, or db.values when reading data.
db.run(statement)| Name | Type | Description |
|---|---|---|
statement | SQL | A Drizzle SQL statement, typically created with the sql template tag. |
Database.RunResult
import { sql } from 'drizzle-orm';
const statement = sql`delete from ${users} where ${users.id} = ${userId}`;
const result = db.run(statement);
Build a parameterized raw SQL statement with Drizzle's sql template and execute it with db.run.
A SQLite write or other command that must run as raw SQL.
A complex statement unsupported by Drizzle's higher-level query builder.
db.run executes a raw parameterized SQL statement against SQLite and returns the driver's run result. It is intended for statements where Drizzle's regular query API cannot express the required SQL.
Follow-up: How would you retrieve rows instead? Use db.all for row objects, db.get for one result, or db.values for value arrays.
The statement is created with Drizzle's sql template, which lets table columns and values participate in the SQL expression. The resulting statement is then passed to db.run.
Follow-up: Why prefer the sql template? It produces a parameterized statement instead of manually concatenating values into SQL.
db.run returns a Database.RunResult, while db.all, db.get, and db.values are the alternatives shown for queries that return data. Therefore, the appropriate method depends on whether the operation is a write or a read.
Follow-up: What should be used for a scalar or single-row lookup? Use db.get for a single returned value or record.
sqldb.alldb.valuesdb.getDatabase.RunResult