Sqlite Storage

作者 val-town2d3ec654b6a7無授權條款收錄於 2026年10月8日更新於 2026年10月8日

Use when a val needs to store structured or relational data. Covers the std/sqlite API, parameterized queries, transactions, and the val-scoped vs organization-scoped database distinction.

AI 產生的概覽

說明如何在 Val Town 的 val 中使用內建 SQLite 儲存結構化或關聯式資料。

功能
這個技能說明如何在 Val Town 中使用 std/sqlite 模組儲存結構化或關聯式資料。內容涵蓋建立資料表與參數化查詢、透過 sqlite.batch 進行原子性的多語句交易,以及 val 範圍與組織範圍資料庫的差異。它也說明透過 sqlite_execute 或 sqlite_batch 工具查詢組織擁有的 val 時如何指定組織,並列出參數化、冪等建表與資料庫遷移的規則。
適用情境
當某個 val 需要持久化的關聯式或結構化資料(例如使用者、資料表或查詢)時使用。它也適合在 val 範圍與組織範圍資料庫之間做選擇,或撰寫交易與結構描述遷移的情境。對於簡單的鍵值資料,技能建議改用 std/blob。
執行需求
需要 Val Town 平台及其內建的 std/sqlite 模組(從 esm.town 匯入);這個技能不含任何指令碼,僅為說明文件,所引用的 API 文件屬於外部資源。

SQLite Storage

Val Town provides built-in SQLite via the std/sqlite module. Reach for it whenever a val needs relational or structured persistent data. For simple key/value data, prefer std/blob instead.

Basic usage

ts
import { sqlite } from "https://esm.town/v/std/sqlite/main.ts";
await sqlite.execute(`CREATE TABLE IF NOT EXISTS users (  id INTEGER PRIMARY KEY,  name TEXT NOT NULL,  email TEXT UNIQUE)`);
await sqlite.execute({  sql: "INSERT INTO users (name, email) VALUES (?, ?)",  args: ["Alice", "[email protected]"],});
const result = await sqlite.execute("SELECT * FROM users");// result.rows = [{ id: 1, name: "Alice", email: "[email protected]" }]

Transactions

Use sqlite.batch for atomic multi-statement transactions — all succeed or all roll back:

ts
await sqlite.batch([  { sql: "INSERT INTO users (name, email) VALUES (?, ?)", args: ["Bob", "[email protected]"] },  { sql: "UPDATE users SET name = ? WHERE id = ?", args: ["Robert", 2] },]);

Per-val vs organization databases

The import path determines which database you get. Both expose the same @libsql/client API (execute, batch) and return rows as keyed objects (Record<string, unknown>[]):

  • std/sqlite/main.ts — val-scoped database, isolated to this val. The default for new vals, and what you almost always want.
  • std/sqlite/global.ts — organization-scoped database, shared across every val owned by the same account. (Your personal account counts as its own organization here, so this database is shared across all of your vals.)

Do not switch an existing val between these import paths — it changes which database the val reads and writes.

Querying org-owned vals via tools

When using the sqlite_execute or sqlite_batch tools to query a val owned by an organization (not your personal account), pass the org handle as the org parameter so the call hits the right database. Example: { sql: "SELECT * FROM users", org: "some-org" }. This only matters for the tool calls — code inside the val itself reads from its own database automatically.

Rules

  • Always use parameterized queries (the args field) for any value derived from user input. Never interpolate strings into SQL.
  • Use CREATE TABLE IF NOT EXISTS so schema setup is idempotent across val restarts.
  • Schema migrations: add new columns with ALTER TABLE ... ADD COLUMN. Wrap in try/catch if the migration may run against an already-updated table.

Reference

Full API docs: https://docs.val.town/reference/std/sqlite/usage/

來源與署名

來源:val-town/plugins位於plugin/skills/sqlite-storage提交2d3ec65

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架