共计 661 个字符,预计需要花费 2 分钟才能阅读完成。
const fetch = require("node-fetch");
const DATABASE_ID = ""; // 替换为你的 D1 数据库 ID
const ACCOUNT_ID = "你的 Cloudflare 账号 ID";
const API_TOKEN = "你的 API 令牌"; // 需要具有 D1 访问权限
const QUERY = "SELECT * FROM my_table;";
async function queryD1() {
const response = await fetch(`https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/d1/database/${DATABASE_ID}/query`, {
method: "POST",
headers: {
"Authorization": `Bearer ${API_TOKEN}`,
"Content-Type": "application/json"
},
body: JSON.stringify({ sql: QUERY })
});
const data = await response.json();
console.log(data);
}
queryD1();
远程链接 D1:
npx wrangler d1 execute dbname --remote --command "SELECT * FROM table;"
远程增加表字段:
npx wrangler d1 execute dbname --remote --command "ALTER TABLE game ADD COLUMN text TEXT;"
正文完
点击链接加入群聊【轩源技术交流群】:https://qm.qq.com/q/xrQ0LCYwTe
