> ## Documentation Index
> Fetch the complete documentation index at: https://docs.archil.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SQLite

> Create and query SQLite databases on an Archil disk

`@archildata/sqlite` creates and queries SQLite databases stored on an Archil disk, with no database server to run. A disk can hold any number of databases, and many clients can use the same database at once, including for writes. This suits AI agents that keep their context as a mix of structured and unstructured data.

Queries run with [serverless execution](https://docs.archil.com/compute/serverless-execution.md) in [regions with compute](https://docs.archil.com/reference/regions.md#compute-availability). Each query counts as one serverless tool call, with execution included; see [Pricing & Billing](https://docs.archil.com/administration/billing.md#serverless-tool-calls).

## Setup

```bash theme={null}
npm install disk @archildata/sqlite
```

The package uses the `disk` SDK's default client, configured from `ARCHIL_API_KEY` and `ARCHIL_REGION` or with `archil.configure()`.

## Create and open databases

A database is a file on the disk, addressed by its path. `createDatabase` creates the file if it doesn't exist and opens it; `getDatabase` opens an existing one.

```ts theme={null}
import * as archil from "disk";
import { createDatabase, getDatabase } from "@archildata/sqlite";

const disk = await archil.getDisk("dsk-0123456789abcdef");
const db = await createDatabase(disk, "apps/app-1/main.sqlite");

// On a later request
const sameDb = await getDatabase(disk, "apps/app-1/main.sqlite");
```

## Run queries

Write queries as tagged templates. Values you interpolate are passed as parameters. Use `read` for queries that only read and `write` for queries that change data, including `RETURNING` queries. Finish a query with `get()` for one row, `all()` for every row, or `run()` for the write result (`changes` and `lastInsertRowid`).

```ts theme={null}
await db.write`CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)`.run();

const name = "Ada Lovelace";
const email = "ada@example.com";
await db.write`INSERT INTO users (name, email) VALUES (${name}, ${email})`.run();

const users = await db.read`SELECT * FROM users`.all();
const ada = await db.read`SELECT * FROM users WHERE email = ${email}`.get();
```

> [!NOTE]
>
> `write` queries need exclusive access to the database file, while many `read` queries run in parallel.

## Transactions

`transaction` runs several queries in one transaction: either all of them apply or none do.

```ts theme={null}
await db.transaction([
  db.write`INSERT INTO users (name, email) VALUES (${"Grace Hopper"}, ${"grace@example.com"})`.run(),
  db.write`INSERT INTO users (name, email) VALUES (${"Alan Turing"}, ${"alan@example.com"})`.run(),
]);
```

## Options

`createDatabase` and `getDatabase` take an options object as their third argument:

| Option | What it does |
| - | - |
| `timeoutMs` | How long a query waits for a locked database, in milliseconds. Default 5000. |
| `readBigInts` | Return integer columns as `bigint`. |
| `returnArrays` | Return rows as arrays instead of objects. |

Failed queries throw `ArchilSQLiteError`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.