Skip to content

Commit ac438d2

Browse files
committed
Editing chapter contents to action feedback.
1 parent 5cc5506 commit ac438d2

File tree

3 files changed

+74
-18
lines changed

3 files changed

+74
-18
lines changed

public/_redirects

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@
258258
/constellation/ /workers-ai/ 301
259259

260260
# D1
261-
/d1/d1-api/ /d1/rest-api/d1-api/ 301
262261
/d1/client-api/ /d1/worker-api/ 301
263262
/d1/build-with-d1/d1-client-api/ /d1/worker-api/ 301
264263
/d1/learning/using-d1-from-pages/ /pages/functions/bindings/#d1-databases 301
@@ -281,7 +280,7 @@
281280
/d1/reference/client-api/ /d1/worker-api/ 301
282281
/d1/reference/environments/ /d1/configuration/environments/ 301
283282
/d1/reference/metrics-analytics/ /d1/observability/metrics-analytics/ 301
284-
/d1/reference/wrangler-commands/ / /d1/rest-api/wrangler-commands/ 301
283+
/d1/reference/wrangler-commands/ / /d1/wrangler-commands/ 301
285284
/d1/how-to/ /d1/build-with-d1/ 301
286285
/d1/how-to/query-databases/ /d1/build-with-d1/query-d1/ 301
287286
/d1/how-to/using-indexes/ /d1/build-with-d1/use-indexes/ 301

src/content/docs/d1/build-with-d1/import-export-data.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ Once you have run the above command, you will need to edit the output SQL file t
105105

106106
You can then follow the steps to [import an existing database](#import-an-existing-database) into D1 by using the `.sql` file you generated from the database dump as the input to `wrangler d1 execute`.
107107

108-
## Foreign key constraints
109-
110-
When importing data, you may need to temporarily disable [foreign key constraints](/d1/build-with-d1/foreign-keys/). To do so, call `PRAGMA defer_foreign_keys = true` before making changes that would violate foreign keys.
111-
112-
Refer to the [foreign key documentation](/d1/build-with-d1/foreign-keys/) to learn more about how to work with foreign keys and D1.
113-
114108
## Export an existing D1 database
115109

116110
In addition to importing existing SQLite databases, you might want to export a D1 database for local development or testing. You can export a D1 database to a `.sql` file using [wrangler d1 export](/workers/wrangler/commands/#export) and then execute (import) with `d1 execute --file`.
@@ -198,6 +192,12 @@ VALUES
198192
('1000', 'Boris Pewter', '2022-12-15 22:16:15');
199193
```
200194

195+
## Foreign key constraints
196+
197+
When importing data, you may need to temporarily disable [foreign key constraints](/d1/build-with-d1/foreign-keys/). To do so, call `PRAGMA defer_foreign_keys = true` before making changes that would violate foreign keys.
198+
199+
Refer to the [foreign key documentation](/d1/build-with-d1/foreign-keys/) to learn more about how to work with foreign keys and D1.
200+
201201
## Next Steps
202202

203203
- Read the SQLite [`CREATE TABLE`](https://www.sqlite.org/lang_createtable.html) documentation.
Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
---
2-
title: Query D1
2+
title: Query a database
33
pcx_content_type: concept
44
sidebar:
55
order: 1
66
---
77

8-
There are three primary ways you can query a D1 database:
8+
There are a number of ways you can interact with a D1 database:
99

1010
1. Using [D1 Workers Binding API](/d1/worker-api/) in your code.
11-
2. Using [D1 REST API](/api/operations/cloudflare-d1-create-database).
12-
3. Using [D1 Wrangler commands](/d1/rest-api/wrangler-commands/).
11+
2. Using [SQL API](/d1/sql-api/sql-statements/).
12+
3. Using [D1 REST API](/api/operations/cloudflare-d1-create-database).
13+
4. Using [D1 Wrangler commands](/d1/wrangler-commands/).
1314

1415
## Query D1 with Workers Binding API
1516

@@ -21,19 +22,75 @@ This requires you to:
2122
2. Prepare a statement.
2223
3. Run the statement.
2324

25+
```js title="index.js"
26+
export default {
27+
async fetch(request, env) {
28+
const {pathname} = new URL(request.url);
29+
const companyName1 = `Bs Beverages`;
30+
const companyName2 = `Around the Horn`;
31+
const stmt = env.DB.prepare(`SELECT * FROM Customers WHERE CompanyName = ?`);
32+
33+
if (pathname === `/RUN`) {
34+
const returnValue = await stmt.bind(companyName1).run();
35+
return Response.json(returnValue);
36+
}
37+
38+
return new Response(
39+
`Welcome to the D1 API Playground!
40+
\nChange the URL to test the various methods inside your index.js file.`,
41+
);
42+
},
43+
};
44+
```
45+
2446
Refer to [Workers Binding API](/d1/worker-api/) for more information.
2547

48+
## Query D1 with SQL API
49+
50+
D1 is compatible with most SQLite's SQL convention since it leverages SQLite's query engine.
51+
52+
```sh
53+
npx wrangler d1 execute prod-d1-tutorial --local --command="SELECT * FROM Customers"
54+
```
55+
56+
```sh output
57+
🌀 Mapping SQL input into an array of statements
58+
🌀 Executing on local database production-db-backend (database-id) from .wrangler/state/v3/d1:
59+
┌────────────┬─────────────────────┬───────────────────┐
60+
│ CustomerId │ CompanyName │ ContactName │
61+
├────────────┼─────────────────────┼───────────────────┤
62+
│ 1 │ Alfreds Futterkiste │ Maria Anders │
63+
├────────────┼─────────────────────┼───────────────────┤
64+
│ 4 │ Around the Horn │ Thomas Hardy │
65+
├────────────┼─────────────────────┼───────────────────┤
66+
│ 11 │ Bs Beverages │ Victoria Ashworth │
67+
├────────────┼─────────────────────┼───────────────────┤
68+
│ 13 │ Bs Beverages │ Random Name │
69+
└────────────┴─────────────────────┴───────────────────┘
70+
```
71+
72+
Refer to [D1 SQL API](/d1/sql-api/sql-statements/) to learn more about supported SQL queries.
73+
2674
## Query D1 with REST API
2775

2876
REST API primarily interacts with the control plane, and allows you to create/manage your D1 database.
2977

30-
You can either use the REST API directly, or use Wrangler commands, which calls the REST API to perform its functions.
78+
Refer to [D1 REST API](/api/operations/cloudflare-d1-create-database) for D1 REST API documentation.
3179

32-
- Refer to [D1 REST API](/api/operations/cloudflare-d1-create-database) for D1 REST API documentation.
33-
- Refer to [D1 Wrangler commands](/d1/rest-api/wrangler-commands/) for the full list of D1 Wrangler commands.
80+
## Query D1 with Wrangler commands
3481

35-
## Query D1 with SQL API
82+
You can use Wrangler commands to interact with the control plane. Note that Wrangler commands use REST APIs to perform its operations.
3683

37-
D1 is compatible with most SQLite's SQL convention since it leverages SQLite's query engine.
84+
```sh
85+
npx wrangler d1 create prod-d1-tutorial
86+
```
87+
88+
```sh output
89+
90+
✅ Successfully created DB 'prod-d1-tutorial'
3891

39-
- Refer to [D1 SQL API](/d1/sql-api/sql-statements/) to learn more about supported SQL queries.
92+
[[d1_databases]]
93+
binding = "DB" # available in your Worker on env.DB
94+
database_name = "prod-d1-tutorial"
95+
database_id = "<unique-ID-for-your-database>"
96+
```

0 commit comments

Comments
 (0)