0Pricing
Edge Computing with Cloudflare Workers & Deno · 课时

在边缘使用 D1 SQL 查询

使用 Cloudflare D1(一种无服务器 SQLite 数据库),从边缘的 Workers 中执行关系型查询。

在边缘使用 D1 SQL 查询 是 CoddyKit 上的免费 Edge Computing with Cloudflare Workers & Deno 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Edge Computing with Cloudflare Workers & Deno 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Edge Computing with Cloudflare Workers & Deno 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What is D1?

Cloudflare D1 is a serverless SQL database built on SQLite. It brings relational queries to the edge, complementing key-value (KV) and Durable Objects.

KV vs D1

When to use which:

  • KV: simple key-value, eventual consistency
  • D1: structured data, joins, transactions, SQL

Creating a Database

Create a D1 database from the CLI, then bind it to your Worker.

wrangler d1 create my-db

Binding D1

Add the binding to wrangler.toml so the database appears on the env object.

# wrangler.toml
[[d1_databases]]
binding = 'DB'
database_name = 'my-db'
database_id = 'xxxx'

Creating a Schema

Define tables with standard SQL, executed via the CLI or a migration file.

CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  email TEXT NOT NULL UNIQUE
);

Prepared Statements

Query D1 from your Worker using prepared statements with bound parameters to prevent SQL injection.

const row = await env.DB
  .prepare('SELECT * FROM users WHERE id = ?')
  .bind(userId)
  .first();

Reading Multiple Rows

Use .all() to fetch every matching row as an array of objects.

const { results } = await env.DB
  .prepare('SELECT email FROM users LIMIT 10')
  .all();

Writing Data

Inserts and updates use the same prepared-statement API with .run().

await env.DB
  .prepare('INSERT INTO users (email) VALUES (?)')
  .bind('a@b.com')
  .run();

Batching Statements

Run several statements atomically in a single round trip with batch, reducing latency.

await env.DB.batch([
  env.DB.prepare('INSERT INTO users (email) VALUES (?)').bind('x@y.com'),
  env.DB.prepare('INSERT INTO users (email) VALUES (?)').bind('p@q.com')
]);

Consistency & Replication

D1 offers strong consistency on its primary, with read replication for low-latency reads near users. Plan writes around the primary location.

When to Choose D1

Pick D1 when you need relational structure, transactions, and SQL at the edge; pick KV for fast simple lookups and Durable Objects for coordinated state.

Quick Check

Test your D1 knowledge.

Recap

You learned to create and bind a D1 database, define schemas, run safe parameterized queries, batch statements, and choose D1 over KV or Durable Objects when you need relational data at the edge.

常见问题解答

「在边缘使用 D1 SQL 查询」课时是免费的吗?

是的 — 「在边缘使用 D1 SQL 查询」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Edge Computing with Cloudflare Workers & Deno 课程的其余内容,请升级到 CoddyKit PRO。 Edge Computing with Cloudflare Workers & Deno 课程共包含 4 节课。

「在边缘使用 D1 SQL 查询」这节课中我会学到什么?

使用 Cloudflare D1(一种无服务器 SQLite 数据库),从边缘的 Workers 中执行关系型查询。 你通过在浏览器中直接运行的动手代码来练习 Edge Computing with Cloudflare Workers & Deno,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Edge Computing with Cloudflare Workers & Deno 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Edge Computing with Cloudflare Workers & Deno 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「在边缘使用 D1 SQL 查询」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Edge Computing with Cloudflare Workers & Deno 课中编写并运行代码吗?

能。每节 Edge Computing with Cloudflare Workers & Deno 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Cloudflare KV 存储
  2. Durable Objects 详解
  3. 将 Deno 与边缘存储集成
  4. 在边缘使用 D1 SQL 查询
← 返回 Edge Computing with Cloudflare Workers & Deno