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
D1 is compatible with most SQLite's SQL convention since it leverages SQLite's query engine. D1 supports a number of database-level statements that allow you to list tables, indexes, and inspect the schema for a given table or index.
11
11
12
-
You can execute any of these statements via the D1 console in the Cloudflare dashboard, [`wrangler d1 execute`](/workers/wrangler/commands/#d1), or with the [D1 Worker Bindings API](/d1/worker-api/d1-binding).
12
+
You can execute any of these statements via the D1 console in the Cloudflare dashboard, [`wrangler d1 execute`](/workers/wrangler/commands/#d1), or with the [D1 Worker Bindings API](/d1/worker-api/d1-database).
- The SQL query you wish to execute on the database.
35
35
36
36
#### Return values
@@ -46,7 +46,7 @@ const stmt = env.DB.prepare("SELECT * FROM Customers WHERE CompanyName = ?").bin
46
46
|`?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`|
47
47
|`?`| 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. |
48
48
49
-
To bind a parameter, use the `stmt.bind()` method.
49
+
To bind a parameter, use the `D1PreparedStatement::bind` method.
50
50
51
51
Order and anonymous examples:
52
52
@@ -91,13 +91,13 @@ const stmt = env.DB.prepare("SELECT * FROM Customers WHERE CompanyName = Bs Beve
91
91
// `stmt` is a static statement.
92
92
```
93
93
94
-
### `db.batch()`
94
+
### `batch()`
95
95
96
96
Sends multiple SQL statements inside a single call to the database. This can have a huge performance impact as it reduces latency from network round trips to D1. D1 operates in auto-commit. Our implementation guarantees that each statement in the list will execute and commit, sequentially, non-concurrently.
97
97
98
98
Batched statements are [SQL transactions](https://www.sqlite.org/lang_transaction.html). If a statement in the sequence fails, then an error is returned for that specific statement, and it aborts or rolls back the entire sequence.
99
99
100
-
To send batch statements, provide `.batch()` a list of prepared statements and get the results in the same order.
100
+
To send batch statements, provide `D1Database::batch` a list of prepared statements and get the results in the same order.
- An array of `D1Result` objects containing the results of the `.db.prepare()` statements. Each object is in the array position corresponding to the array position of the initial `db.prepare()` statement within the `statementArray`.
120
+
- An array of `D1Result` objects containing the results of the `D1Database::prepare` statements. Each object is in the array position corresponding to the array position of the initial `D1Database::prepare` statement within the `statements`.
121
121
- Refer to [`D1Result`](/d1/worker-api/return-object/#d1result) for more information about this object.
122
122
123
123
<Detailsheader="Example of return values"open={false}>
@@ -209,7 +209,7 @@ console.log(stmt[1].results);
209
209
returnResponse.json(batchResult);
210
210
```
211
211
212
-
### `db.exec()`
212
+
### `exec()`
213
213
214
214
Executes one or more queries directly without prepared statements or parameter bindings.
215
215
@@ -219,7 +219,7 @@ const returnValue = await env.DB.exec(`SELECT * FROM Customers WHERE CompanyName
You can execute SQL queries on your D1 database from a Worker using the Worker Binding API. To do this, you can perform the following steps:
11
11
12
-
1.[Bind the D1 Database](/d1/worker-api/d1-binding).
13
-
2.[Prepare a statement](/d1/worker-api/d1-binding/#dbprepare).
12
+
1.[Bind the D1 Database](/d1/worker-api/d1-database).
13
+
2.[Prepare a statement](/d1/worker-api/d1-database/#dbprepare).
14
14
3.[Run the prepared statement](/d1/worker-api/prepared-statements).
15
15
4. Analyze the [return object](/d1/worker-api/return-object) (if necessary).
16
16
@@ -20,9 +20,9 @@ Refer to the relevant sections for the API documentation.
20
20
21
21
D1 Worker Bindings API is fully-typed via the [`@cloudflare/workers-types`](/workers/languages/typescript/#typescript) package, and also supports [generic types](https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-types) as part of its TypeScript API. A generic type allows you to provide an optional `type parameter` so that a function understands the type of the data it is handling.
22
22
23
-
When using the [query statement methods](/d1/worker-api/prepared-statements)`stmt.run()`, `stmt.raw()` and `stmt.first()`, you can provide a type representing each database row. D1's API will [return the result object](#return-object) with the correct type.
23
+
When using the [query statement methods](/d1/worker-api/prepared-statements)`D1PreparedStatement::run`, `D1PreparedStatement::raw` and `D1PreparedStatement::first`, you can provide a type representing each database row. D1's API will [return the result object](#return-object) with the correct type.
24
24
25
-
For example, providing an `OrderRow` type as a type parameter to `stmt.run()` will return a typed `Array<OrderRow>` object instead of the default `Record<string, unknown>` type:
25
+
For example, providing an `OrderRow` type as a type parameter to `D1PreparedStatement::run` will return a typed `Array<OrderRow>` object instead of the default `Record<string, unknown>` type:
This chapter documents the various ways you can run and retrieve the results of a query after you have [prepared your statement](/d1/worker-api/d1-binding/#dbprepare).
10
+
This chapter documents the various ways you can run and retrieve the results of a query after you have [prepared your statement](/d1/worker-api/d1-database/#dbprepare).
11
11
12
12
## Methods
13
13
14
-
### `stmt.run()`
14
+
### `run()`
15
15
16
16
Runs the prepared query (or queries) and returns results. The returned results includes metadata.
-`results` is empty for write operations such as `UPDATE`, `DELETE`, or `INSERT`.
73
-
- 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.
74
-
-`stmt.run()` is functionally equivalent to `stmt.all()`, and can be treated as an alias.
73
+
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `D1PreparedStatement::run` to return a typed result object.
74
+
-`D1PreparedStatement::run` is functionally equivalent to `D1PreparedStatement::all`, and can be treated as an alias.
75
75
- You can choose to extract only the results you expect from the statement by simply returning the `results` property of the return object.
76
76
77
77
<Detailsheader="Example of returning only the `results`"open={false}>
- 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.
161
+
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `D1PreparedStatement::raw` to return a typed result array.
162
162
163
-
### `stmt.first()`
163
+
### `first()`
164
164
165
165
Runs the prepared query (or queries), and returns the first row of the query result as an object. This does not return any metadata. Instead, it directly returns the object.
- If the query returns rows but `column` does not exist, then `first()` throws the `D1_ERROR` exception.
220
-
-`stmt.first()` does not alter the SQL query. To improve performance, consider appending `LIMIT 1` to your statement.
221
-
- 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.
220
+
- If the query returns rows but `column` does not exist, then `D1PreparedStatement::first` throws the `D1_ERROR` exception.
221
+
-`D1PreparedStatement::first` does not alter the SQL query. To improve performance, consider appending `LIMIT 1` to your statement.
222
+
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `D1PreparedStatement::first` to return a typed result object.
0 commit comments