在 Windows 上指定 Sequelize sqlite 路径
Posted
技术标签:
【中文标题】在 Windows 上指定 Sequelize sqlite 路径【英文标题】:Specify Sequelize sqlite path on Windows 【发布时间】:2018-12-10 00:19:03 【问题描述】:我一直在使用 Sequelize 和 sqlite3 在 Linux 中开发一个 Electron 应用程序,并且一切正常。我连接到这样的数据库:
new Sequelize("sqlite:" + myPath);
myPath 的格式为 '/home/.../someDB.db'
但是,我尝试在 Windows 上运行我的项目,但在连接数据库时遇到了问题。
如果我对 myPath 使用“someDB.DB”,我可以连接到数据库,但是当 myPath 是“C:...\someDB.db”形式的绝对路径时,我会收到错误:
C:\projectPath\node_modules\bluebird\js\release\debuggability.js:868 未处理的拒绝 SequelizeConnectionError: SQLITE_CANTOPEN: 无法打开数据库文件 在 Database.connections.(匿名函数).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 (匿名)@ C:\projectPath\node_modules\bluebird\js\release\debuggability.js:43 设置超时(异步) 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 异步调用 调度@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 异步调用 调度@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 异步调用 调度@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 (匿名)@ C:\projectPath\node_modules\bluebird\js\release\promise.js:486 连接。(匿名函数).lib.Database.err @C:\projectPath\node_modules\sequelize\lib\dialects\sqlite\connection-manager.js:66
我应该如何在 Windows 上使用 Sequelize 连接到 sqlite 数据库?
【问题讨论】:
【参考方案1】:我意识到解决方案就是使用更详细的构造函数:
new Sequelize('', '', '',
dialect: 'sqlite',
storage: myPath
);
【讨论】:
以上是关于在 Windows 上指定 Sequelize sqlite 路径的主要内容,如果未能解决你的问题,请参考以下文章
使用 Sequelize (NodeJS) 而不是 * 指定特定字段