导出现有的 D1 数据库
除了导入现有的 SQLite 数据库之外,您可能还希望导出 D1 数据库以进行本地开发或测试。您可以使用 wrangler d1 export 将 D1 数据库导出到文件,然后使用 ..sql``d1 execute --file
要导出完整的 D1 数据库架构和数据,请执行以下操作:
终端窗口
npx wrangler d1 export <database_name> --remote --output=./database.sql
要导出单个表 Schema 和数据:
终端窗口
npx wrangler d1 export <database_name> --remote --table=<table_name> --output=./table.sql
要仅导出 D1 数据库架构:
终端窗口
npx wrangler d1 export <database_name> --remote --output=./schema.sql --no-data
要仅导出 D1 表架构:
终端窗口
npx wrangler d1 export <database_name> --remote --table=<table_name> --output=./schema.sql --no-data
要仅导出 D1 数据库数据:
终端窗口
npx wrangler d1 export <database_name> --remote --output=./data.sql --no-schema
要仅导出 D1 表数据:
终端窗口
npx wrangler d1 export <database_name> --remote --table=<table_name> --output=./data.sql --no-schema
已知限制
- 虚拟表不支持导出,包括具有虚拟表的数据库。D1 支持使用 SQLite 的 FTS5 模块↗进行全文搜索的虚拟表。解决方法是删除所有虚拟表,导出,然后重新创建虚拟表。
- 正在运行的导出将阻止其他数据库请求。
故障 排除
如果您在尝试将现有架构和/或数据集导入 D1 时收到错误:
- 确保以 SQL 格式(通常带有文件扩展名)导入数据。如果您有数据库转储,请参阅如何转换 SQLite 文件。
.sql``.sqlite3
- 确保架构与 SQLite3 ↗ 兼容。您无法将数据从 MySQL 或 PostgreSQL 数据库导入 D1,因为类型和 SQL 语法不直接兼容。
- 如果表之间有外键关系,请确保按正确的顺序导入表。您不能引用尚不存在的表。
- 如果您收到错误,请确保您已从转储的 SQL 语句中删除 and。
"cannot start a transaction within a transaction"``BEGIN TRANSACTION``COMMIT
解决错误Statement too long
如果您在尝试将大型 SQL 文件导入 D1 时遇到错误,则表示文件中的某个 SQL 语句超过了允许的最大长度。Statement too long
要解决此问题,请将单个 large 语句转换为多个 smaller 语句。例如,不要在一个语句中插入 1,000 行,而是将其拆分为四组,每组 250 行,如下面的代码所示。INSERT``INSERT
以前:
INSERT INTO users (id, full_name, created_on)VALUES ('1', 'Jacquelin Elara', '2022-08-20 05:39:52'), ('2', 'Hubert Simmons', '2022-12-15 21:56:13'), ... ('1000', 'Boris Pewter', '2022-12-24 07:59:54');
后:
INSERT INTO users (id, full_name, created_on)VALUES ('1', 'Jacquelin Elara', '2022-08-20 05:39:52'), ... ('100', 'Eddy Orelo', '2022-12-15 22:16:15');...INSERT INTO users (id, full_name, created_on)VALUES ('901', 'Roran Eroi', '2022-08-20 05:39:52'), ... ('1000', 'Boris Pewter', '2022-12-15 22:16:15');
外键约束
导入数据时,您可能需要暂时禁用外键约束。为此,请在进行违反外键的更改之前调用 。PRAGMA defer_foreign_keys = true
请参阅 外键文档 了解有关如何使用外键和 D1 的更多信息。
后续步骤
- 阅读 SQLite
CREATE TABLE
↗ 文档。 - 了解如何在 Worker 中使用 D1 Workers Binding API。
- 了解数据库迁移如何与 D1 配合使用。
Was this helpful?
Yes No
What did you like?
Accurate
Easy to understand
Solved my problem
Helped me decide to use the product
Other
What went wrong?
Hard to understand
Incorrect information
Missing the information
Other
Thank you for helping improve Cloudflare's documentation!
class r extends HTMLElement{connectedCallback(){const d={'[data-icon="material-symbols:thumb-up-outline-rounded"]':"#feedback-yes",'[data-icon="material-symbols:thumb-down-outline-rounded"]':"#feedback-no"},s=this.querySelector("#feedback-thumbs"),o=this.querySelector("#feedback-thanks");if(!(!s||!o))for(const[i,n]of Object.entries(d))this.querySelector(i)?.addEventListener("click",()=>{s.classList.add("hidden");const e=this.querySelector(n);if(!e)return;e.classList.remove("hidden");const a=e.querySelector("form");a&&a.addEventListener("submit",async c=>{c.preventDefault();const t=new FormData(c.target);t.set("page",window.location.pathname),t.set("option",n.split("-")[1]),fetch("https://feedback.developers.cloudflare.com",{method:"POST",body:t}),e.classList.add("hidden"),o.classList.remove("hidden")})})}}customElements.define("feedback-prompt",r);
编辑页面
Import and export data