我一直在 Linux 中使用 Sequelize 和 sqlite3 开发一个 Electron 应用程序,一切都运行良好。我像这样连接到数据库:
new Sequelize("sqlite:" + myPath);
其中 myPath 的形式为“/home/.../someDB.db”
但是,我尝试在 Windows 上运行我的项目,但遇到了连接数据库的问题。
如果我使用“someDB.DB”作为 myPath,我可以连接到数据库,但是当 myPath 是“C:...\someDB.db”形式的绝对路径时,我会收到错误:
C:\projectPath\node_modules\bluebird\js\release\debuggability.js:868 Unhandled rejection SequelizeConnectionError: SQLITE_CANTOPEN: unable to open database file at Database.connections.(anonymous function).lib.Database.err (C:\projectPath\node_modules\sequelize\lib\dialects\sqlite\connection-manager.js:66:63) printWarning @ C:\projectPath\node_modules\bluebird\js\release\debuggability.js:868 formatAndLogError @ C:\projectPath\node_modules\bluebird\js\release\debuggability.js:593 fireRejectionEvent @ C:\projectPath\node_modules\bluebird\js\release\debuggability.js:618 Promise._notifyUnhandledRejection @ C:\projectPath\node_modules\bluebird\js\release\debuggability.js:64 (anonymous) @ C:\projectPath\node_modules\bluebird\js\release\debuggability.js:43 setTimeout (async) Promise._ensurePossibleRejectionHandled @ C:\projectPath\node_modules\bluebird\js\release\debuggability.js:42 Promise._reject @ C:\projectPath\node_modules\bluebird\js\release\promise.js:658 Promise._settlePromise @ C:\projectPath\node_modules\bluebird\js\release\promise.js:584 Promise._settlePromise0 @ C:\projectPath\node_modules\bluebird\js\release\promise.js:614 Promise._settlePromises @ C:\projectPath\node_modules\bluebird\js\release\promise.js:689 Async._drainQueue @ C:\projectPath\node_modules\bluebird\js\release\async.js:133 Async._drainQueues @ C:\projectPath\node_modules\bluebird\js\release\async.js:143 Async.drainQueues @ C:\projectPath\node_modules\bluebird\js\release\async.js:17 Async Call schedule @ C:\projectPath\node_modules\bluebird\js\release\schedule.js:18 Async._queueTick @ C:\projectPath\node_modules\bluebird\js\release\async.js:152 AsyncSettlePromises @ C:\projectPath\node_modules\bluebird\js\release\async.js:88 Promise._reject @ C:\projectPath\node_modules\bluebird\js\release\promise.js:656 Promise._settlePromise @ C:\projectPath\node_modules\bluebird\js\release\promise.js:566 Promise._settlePromise0 @ C:\projectPath\node_modules\bluebird\js\release\promise.js:614 Promise._settlePromises @ C:\projectPath\node_modules\bluebird\js\release\promise.js:689 Async._drainQueue @ C:\projectPath\node_modules\bluebird\js\release\async.js:133 Async._drainQueues @ C:\projectPath\node_modules\bluebird\js\release\async.js:143 Async.drainQueues @ C:\projectPath\node_modules\bluebird\js\release\async.js:17 Async Call schedule @ C:\projectPath\node_modules\bluebird\js\release\schedule.js:18 Async._queueTick @ C:\projectPath\node_modules\bluebird\js\release\async.js:152 AsyncSettlePromises @ C:\projectPath\node_modules\bluebird\js\release\async.js:88 Promise._reject @ C:\projectPath\node_modules\bluebird\js\release\promise.js:656 Promise._settlePromise @ C:\projectPath\node_modules\bluebird\js\release\promise.js:566 Promise._settlePromise0 @ C:\projectPath\node_modules\bluebird\js\release\promise.js:614 Promise._settlePromises @ C:\projectPath\node_modules\bluebird\js\release\promise.js:689 Async._drainQueue @ C:\projectPath\node_modules\bluebird\js\release\async.js:133 Async._drainQueues @ C:\projectPath\node_modules\bluebird\js\release\async.js:143 Async.drainQueues @ C:\projectPath\node_modules\bluebird\js\release\async.js:17 Async Call schedule @ C:\projectPath\node_modules\bluebird\js\release\schedule.js:18 Async._queueTick @ C:\projectPath\node_modules\bluebird\js\release\async.js:152 AsyncSettlePromises @ C:\projectPath\node_modules\bluebird\js\release\async.js:88 Promise._reject @ C:\projectPath\node_modules\bluebird\js\release\promise.js:656 Promise._rejectCallback @ C:\projectPath\node_modules\bluebird\js\release\promise.js:474 (anonymous) @ C:\projectPath\node_modules\bluebird\js\release\promise.js:486 connections.(anonymous function).lib.Database.err @ C:\projectPath\node_modules\sequelize\lib\dialects\sqlite\connection-manager.js:66
我应该如何在 Windows 上使用 Sequelize 连接到 sqlite 数据库?
最佳答案
我意识到解决方案只是使用更详细的构造函数:
new Sequelize('', '', '', {
dialect: 'sqlite',
storage: myPath
});
关于javascript - 在 Windows 上指定 Sequelize sqlite 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51124850/