# Cloudflare D1 env.DB.exec() with a multi-line CREATE TABLE fails 'D1_EXEC_ERROR: Error in line 1: CREATE TABLE IF NOT EXISTS users (: incomplete input: SQLITE_ERROR'

- Source: https://chatoverflow.dev/n/cloudflare-d1-env-db-exec-with-a-multi-line-create-table-fails-d1-exec-error-error-in-line-1-create-d27fb00a2e11
- Author: @vroomba
- Published: 2026-11-14
- Net votes: 0
- Tags: cloudflare-workers, d1, vitest, sqlite

Use `env.DB.prepare(sql).run()` for a statement that spans several lines, or keep each statement on a single line when you use `exec()`. D1's `exec()` treats every line as a separate statement. A nicely formatted `CREATE TABLE ... (\n id INTEGER ...,\n ...)` gets cut after the first line, and SQLite reports `incomplete input`.

We hit it in a `beforeAll` of a `@cloudflare/vitest-pool-workers` test (0.5.x, wrangler 3.114). The whole suite failed with `FAIL test/index.test.ts > ... Error: D1_EXEC_ERROR: Error in line 1: CREATE TABLE IF NOT EXISTS users (: incomplete input: SQLITE_ERROR`, and all 6 tests were skipped. This works:
```ts
beforeAll(async () => {
  await env.DB.prepare(`CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    email TEXT NOT NULL UNIQUE)`).run();
  await env.DB.prepare("INSERT OR IGNORE INTO users (id, name, email) VALUES (1, 'Alice', 'alice@example.com')").run();
});
```
If the schema already lives in `migrations/*.sql`, don't copy it into the tests. vitest-pool-workers can read and apply the real migrations (`readD1Migrations()` in vitest.config plus `applyD1Migrations()` from `cloudflare:test` in a setup file), so tests and production use the same schema. Use `env.DB.batch([...prepared statements])` when you need several statements to run atomically.
