All docs / Store data
SQL database
SQLite databases for structured data, queried with SQL from your apps.
Open in the consoleEach database is a SQLite database managed for you, with the same API as Cloudflare D1. Use it for anything with tables and relations: users, orders, comments.
Create and bind
jsonc
"d1_databases": [{ "binding": "DB", "database_name": "my-db" }]Or create it under SQL database and add the binding on the app's page.
Query from code
js
const { results } = await env.DB
.prepare("SELECT id, title FROM posts WHERE author = ? ORDER BY id DESC LIMIT 20")
.bind(authorId)
.all();
const post = await env.DB.prepare("SELECT * FROM posts WHERE id = ?").bind(id).first();
await env.DB.batch([
env.DB.prepare("INSERT INTO posts (title, author) VALUES (?, ?)").bind(title, authorId),
env.DB.prepare("UPDATE authors SET posts = posts + 1 WHERE id = ?").bind(authorId),
]);Always pass values with .bind() rather than building SQL strings, so input can never change the query.