{"record":{"id":"6b5128b49ae58778","repo":"n8n-io/n8n","slug":"could-not-connect-to-database","errorCode":null,"errorMessage":"Could not connect to database","messagePattern":"Could not connect to database","errorType":"exception","errorClass":"NodeOperationError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/nodes-langchain/nodes/agents/Agent/agents/SqlAgent/other/handlers/sqlite.ts","lineNumber":40,"sourceCode":"\t\tconst stream = await this.helpers.getBinaryStream(binaryData.id, chunkSize);\n\t\tconst buffer = await this.helpers.binaryToBuffer(stream);\n\t\tfileBase64 = buffer.toString('base64');\n\t} else {\n\t\tfileBase64 = binaryData.data;\n\t}\n\n\tconst bufferString = Buffer.from(fileBase64, BINARY_ENCODING);\n\n\t// Track and cleanup temp files at exit\n\ttemp.track();\n\n\tconst tempDbPath = temp.path({ suffix: '.sqlite' });\n\tfs.writeFileSync(tempDbPath, bufferString);\n\n\t// Initialize a new SQLite database from the temp file\n\tconst tempDb = new sqlite3.Database(tempDbPath, (error: Error | null) => {\n\t\tif (error) {\n\t\t\tthrow new NodeOperationError(this.getNode(), 'Could not connect to database');\n\t\t}\n\t});\n\ttempDb.close();\n\n\treturn new DataSource({\n\t\ttype: 'sqlite',\n\t\tdatabase: tempDbPath,\n\t});\n}\n","sourceCodeStart":22,"sourceCodeEnd":50,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/nodes-langchain/nodes/agents/Agent/agents/SqlAgent/other/handlers/sqlite.ts#L22-L50","documentation":"Thrown by the sqlite3 driver's connection callback inside getSqliteDataSource when opening the temp .sqlite file fails. Note: the throw happens inside an async sqlite3 callback, so it will NOT propagate cleanly to the caller — this is a latent bug; the promise resolves and the failure surfaces later as a query error or undefined behavior.","triggerScenarios":"The decoded base64 buffer is not a valid SQLite database file; the temp path is unwritable; the file is locked or truncated.","commonSituations":"Upstream node delivered a non-SQLite binary (e.g. a CSV, JSON, or corrupt blob) but the workflow expects .sqlite; the file was partially downloaded; disk-full or permissions issue on the temp directory.","solutions":["Verify the binary input is genuinely a SQLite database file (check the magic header 'SQLite format 3').","If the data is CSV/JSON, convert it to SQLite first with a Code node or a dedicated converter.","Ensure the n8n process has write access to the system temp directory."],"exampleFix":"// before — non-SQLite binary reaches the agent\n// (no validation; opaque downstream failure)\n\n// after — validate the SQLite magic header before calling the agent\nconst b = items[0].binary?.data;\nif (b) {\n  const buf = Buffer.from(await this.helpers.getBinaryDataBuffer(b.id));\n  if (buf.subarray(0, 15).toString('ascii') !== 'SQLite format 3\\u0000') {\n    throw new NodeOperationError(this.getNode(), 'Input is not a valid SQLite file');\n  }\n}","handlingStrategy":"validation","validationCode":"// Validate the SQLite magic header before the agent runs\nconst b = items[i].binary?.[binaryPropertyName];\nif (b) {\n  const buf = b.id\n    ? await this.helpers.binaryToBuffer(await this.helpers.getBinaryStream(b.id))\n    : Buffer.from(b.data, 'base64');\n  if (buf.subarray(0, 15).toString('ascii') !== 'SQLite format 3\\u0000') {\n    throw new NodeOperationError(this.getNode(), 'Input binary is not a valid SQLite database file');\n  }\n}","typeGuard":"function isSqliteBuffer(buf: Buffer): boolean {\n  return buf.subarray(0, 15).toString('ascii') === 'SQLite format 3\\u0000';\n}","tryCatchPattern":null,"preventionTips":["Verify the upstream node outputs a genuine .sqlite file (check magic header).","Convert CSV/JSON to SQLite upstream if needed.","Be aware the current callback-based throw is a latent bug — surface validation upstream to avoid opaque failures."],"tags":["langchain","agent","sql","sqlite","binary","validation","bug"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}