You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/d1/worker-api/database.mdx
+34-3Lines changed: 34 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,14 +21,16 @@ Example of a prepared statement:
21
21
22
22
```js
23
23
conststmt=db.prepare("SELECT * FROM users WHERE name = ?1").bind(someVariable);
24
-
// someVariable will replace the placeholder '?1' in the query.
24
+
// A variable (someVariable) will replace the placeholder '?1' in the query.
25
+
// This is a prepared statement.
25
26
```
26
27
27
28
Example of a static statement:
28
29
29
30
```js
30
31
conststmt=db.prepare('SELECT * FROM users WHERE name = "John Doe"');
31
-
// "John Doe" is hard-coded into the query. This is a static statement.
32
+
// "John Doe" is hard-coded into the query.
33
+
// This is a static statement.
32
34
```
33
35
34
36
#### Parameters
@@ -49,6 +51,32 @@ const stmt = db.prepare('SELECT * FROM users WHERE name = "John Doe"');
49
51
```js
50
52
conststmt=db.prepare(`SELECT * FROM users WHERE name = "Anthony"; SELECT * FROM users WHERE name = ?1`).bind("Joe")
51
53
```
54
+
- D1 follows the [SQLite convention](https://www.sqlite.org/lang_expr.html#varparam) for prepared statements parameter binding. Currently, D1 only supports Ordered (`?NNNN`) and Anonymous (`?`) parameters. In the future, D1 will support named parameters as well.
|`?NNN`| Ordered | A question mark followed by a number `NNN` holds a spot for the `NNN`-th parameter. `NNN` must be between `1` and `SQLITE_MAX_VARIABLE_NUMBER`|
59
+
|`?`| Anonymous | A question mark that is not followed by a number creates a parameter with a number one greater than the largest parameter number already assigned. If this means the parameter number is greater than SQLITE_MAX_VARIABLE_NUMBER, it is an error. This parameter format is provided for compatibility with other database engines. But because it is easy to miscount the question marks, the use of this parameter format is discouraged. Programmers are encouraged to use one of the symbolic formats below or the `?NNN` format above instead |
60
+
61
+
To bind a parameter, use the `stmt.bind()` method.
62
+
63
+
Order and anonymous examples:
64
+
65
+
```js
66
+
conststmt=db.prepare("SELECT * FROM users WHERE name = ?").bind("John Doe");
67
+
```
68
+
69
+
```js
70
+
conststmt= db
71
+
.prepare("SELECT * FROM users WHERE name = ? AND age = ?")
72
+
.bind("John Doe", 41);
73
+
```
74
+
75
+
```js
76
+
conststmt= db
77
+
.prepare("SELECT * FROM users WHERE name = ?2 AND age = ?1")
78
+
.bind(41, "John Doe");
79
+
```
52
80
53
81
### `db.batch()`
54
82
@@ -123,7 +151,7 @@ console.log(rows[1].results);
123
151
124
152
### `db.exec()`
125
153
126
-
Executes one or more queries directly without prepared statements or parameters binding. This method can have poorer performance (prepared statements can be reused in some cases) and, more importantly, is less safe. Only use this method for maintenance and one-shot tasks (for example, migration jobs). The input can be one or multiple queries separated by `\n`.
154
+
Executes one or more queries directly without prepared statements or parameters binding.
127
155
128
156
```js
129
157
constmigration=awaitfetch("/migration.sql");
@@ -156,6 +184,9 @@ console.log(out);
156
184
#### Guidance
157
185
158
186
- If an error occurs, an exception is thrown with the query and error messages, execution stops and further statements are not executed. Refer to [Errors](/d1/build-with-d1/d1-client-api/#errors) to learn more.
187
+
- This method can have poorer performance (prepared statements can be reused in some cases) and, more importantly, is less safe.
188
+
- Only use this method for maintenance and one-shot tasks (for example, migration jobs).
189
+
- The input can be one or multiple queries separated by `\n`.
Copy file name to clipboardExpand all lines: src/content/docs/d1/worker-api/query.mdx
+31-98Lines changed: 31 additions & 98 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
title: D1 Query
2
+
title: D1 query
3
3
pcx_content_type: concept
4
4
sidebar:
5
5
order: 2
@@ -13,32 +13,30 @@ You can modify the query results which has been obtained after executing a `.db(
13
13
14
14
## Methods
15
15
16
-
### await stmt.all()
16
+
### `await stmt.run()`
17
17
18
-
Returns all rows as an array of objects, with each result row represented as an object on the `results` property of the `D1Result` type.
18
+
Runs the query (or queries) and returns results.
19
19
20
20
```js
21
-
const { results } =awaitstmt.all();
21
+
const { results } =awaitstmt.run();
22
22
```
23
23
24
-
#### Parameters
24
+
#### Parameter
25
25
26
26
- None.
27
27
28
-
#### Return values
28
+
#### Return value
29
29
30
-
- <code>Array</code>: <Typetext="Array"/>
31
-
- An array of objects.
32
-
- Each row represents a row.
33
-
- Each object is created on the `results` property of the `D1Result` type.
30
+
- <code>results</code>: <Typetext="Array"/>
31
+
- An array of objects, where each object represents a row of the query result.
32
+
- Each object is created on the `results` property of the `D1Result` type. {/*What does it return when `results` is actually empty?*/}
34
33
35
34
<Detailsheader="Example of return values"open={false}>
36
35
```js
37
-
conststmt=db.prepare("SELECT name, age FROM users LIMIT 3");
38
-
const { results } =awaitstmt.all();
36
+
conststmt=awaitdb.prepare("SELECT name, age FROM users LIMIT 3");
37
+
const { results } =awaitstmt.run();
39
38
console.log(results);
40
39
```
41
-
42
40
```js output
43
41
[
44
42
{
@@ -59,10 +57,12 @@ console.log(results);
59
57
60
58
#### Guidance
61
59
62
-
- When joining tables with identical column names, only the leftmost column will be included in the row object. Use [`stmt.raw()`](#await-stmtraw) to return all rows as an array of arrays.
63
-
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `all()` to return a typed result object.
60
+
-`results` is empty for write operations such as UPDATE, DELETE, or INSERT.
61
+
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `run()` to return a typed result object.
62
+
-`stmt.run()` is functionally equivalent to `stmt.all()` and can be treated as an alias.
63
+
-
64
64
65
-
### await stmt.raw()
65
+
### `await stmt.raw()`
66
66
67
67
Returns results as an array of arrays, with each row represented by an array. The return type is an array of arrays, and does not include query metadata.
68
68
@@ -112,7 +112,7 @@ console.log(columns);
112
112
113
113
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `raw()` to return a typed result array.
114
114
115
-
### await stmt.first()
115
+
### `await stmt.first()`
116
116
117
117
Returns the first row of the query result. This does not return metadata like the other methods. Instead, it directly returns the object.
118
118
@@ -167,30 +167,32 @@ console.log(total);
167
167
-`stmt.first()` does not alter the SQL query. To improve performance, consider appending `LIMIT 1` to your statement.
168
168
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `first()` to return a typed result object.
169
169
170
-
### await stmt.run()
170
+
{/*### `stmt.all()`
171
171
172
-
Runs the query (or queries) and returns results.
172
+
Returns all rows as an array of objects, with each result row represented as an object on the `results` property of the `D1Result` type.
173
173
174
174
```js
175
-
const { results } =awaitstmt.run();
175
+
const { results } = await stmt.all();
176
176
```
177
177
178
-
#### Parameter
178
+
#### Parameters
179
179
180
180
- None.
181
181
182
-
#### Return value
182
+
#### Return values
183
183
184
-
- <code>results</code>: <Typetext="Array"/>
185
-
- An array of objects, where each object represents a row of the query result.
186
-
- Each object is created on the `results` property of the `D1Result` type. {/*What does it return when `results` is actually empty?*/}
184
+
- <code>Array</code>: <Type text="Array"/>
185
+
- An array of objects.
186
+
- Each row represents a row.
187
+
- Each object is created on the `results` property of the `D1Result` type.
187
188
188
189
<Details header="Example of return values" open = {false}>
189
190
```js
190
-
conststmt=awaitdb.prepare("SELECT name, age FROM users LIMIT 3");
191
-
const { results } =awaitstmt.run();
191
+
const stmt = db.prepare("SELECT name, age FROM users LIMIT 3");
192
+
const { results } = await stmt.all();
192
193
console.log(results);
193
194
```
195
+
194
196
```js output
195
197
[
196
198
{
@@ -211,74 +213,5 @@ console.log(results);
211
213
212
214
#### Guidance
213
215
214
-
-`results` is empty for write operations such as UPDATE, DELETE, or INSERT.
215
-
- Run is functionally equivalent to `stmt.all()` and can be treated as an alias.
216
-
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `run()` to return a typed result object.
217
-
218
-
### await db.dump()
219
-
220
-
:::caution
221
-
This API only works on databases created during D1's alpha period. Check which version your database uses with `wrangler d1 info <DATABASE_NAME>`.
222
-
:::
223
-
224
-
Dumps the entire D1 database to an SQLite compatible file inside an ArrayBuffer.
225
-
226
-
```js
227
-
constdump=awaitdb.dump();
228
-
returnnewResponse(dump, {
229
-
status:200,
230
-
headers: {
231
-
"Content-Type":"application/octet-stream",
232
-
},
233
-
});
234
-
```
235
-
236
-
#### Parameters
237
-
238
-
- None.
239
-
240
-
#### Return values
241
-
242
-
- ???
243
-
244
-
#### Guidance
245
-
246
-
- ???
247
-
248
-
### await db.exec()
249
-
250
-
Executes one or more queries directly without prepared statements or parameters binding.
251
-
252
-
```js
253
-
constout=awaitdb.exec();
254
-
```
255
-
256
-
#### Parameters
257
-
258
-
- ???
259
-
260
-
#### Return values
261
-
262
-
- ???
263
-
264
-
<Detailsheader="Example of return values"open={false}>
265
-
```js
266
-
constmigration=awaitfetch("/migration.sql");
267
-
constout=awaitdb.exec(migration.text());
268
-
console.log(out);
269
-
```
270
-
```js output
271
-
{
272
-
count:80,
273
-
duration:76
274
-
}
275
-
```
276
-
</Details>
277
-
278
-
#### Guidance
279
-
280
-
- This method can have poorer performance (prepared statements can be reused in some cases) and, more importantly, is less safe.
281
-
- Only use this method for maintenance and one-shot tasks (for example, migration jobs).
282
-
- The input can be one or multiple queries separated by `\n`.
283
-
- If an error occurs, an exception is thrown with the query and error messages, execution stops and further statements are not executed. Refer to [Errors](/d1/build-with-d1/d1-client-api/#errors) to learn more.
284
-
216
+
- When joining tables with identical column names, only the leftmost column will be included in the row object. Use [`stmt.raw()`](#await-stmtraw) to return all rows as an array of arrays.
217
+
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `all()` to return a typed result object. */}
0 commit comments