简体   繁体   English

用 sqlite3 连接电子

[英]Connecting electron with sqlite3

I want to connect electron with sqlite3.我想将电子与 sqlite3 连接起来。 My package.json file is listed below下面列出了我的 package.json 文件

{
  "name": "electrontest2",
  "version": "1.0.0",
  "description": "",
  "main": "db.js",
  "scripts": {
    "start": "electron ."
  },

"scripts": {
"start": "electron ."
},
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "electron-prebuilt": "^1.2.2"
  },
  "dependencies": {
    "jquery": "^2.1.4",
    "sqlite3": "^3.1.4"
  }
}

But while doing npm start it is throwing this error但是在执行 npm start 时,它会抛出此错误

App threw an error during load
Error: Cannot find module '/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/binding/electron-v1.2-linux-x64/node_sqlite3.node'
    at Module._resolveFilename (module.js:438:15)
    at Function.Module._resolveFilename (/var/www/tools/node/project/electrontest2/node_modules/electron-prebuilt/dist/resources/electron.asar/common/reset-search-paths.js:47:12)
    at Function.Module._load (module.js:386:25)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/sqlite3.js:4:15)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module '/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/binding/electron-v1.2-linux-x64/node_sqlite3.node'
    at Module._resolveFilename (module.js:438:15)
    at Function.Module._resolveFilename (/var/www/tools/node/project/electrontest2/node_modules/electron-prebuilt/dist/resources/electron.asar/common/reset-search-paths.js:47:12)
    at Function.Module._load (module.js:386:25)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/sqlite3.js:4:15)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)

I think I am getting this error as i am having :我想我遇到了这个错误,因为我有:

'/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/binding/node-v46-linux-x64/node_sqlite3.node' '/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/binding/node-v46-linux-x64/node_sqlite3.node'

instead of而不是

'/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/binding/electron-v1.2-linux-x64/node_sqlite3.node' '/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/binding/electron-v1.2-linux-x64/node_sqlite3.node'

Please tell where I am making mistake.请告诉我哪里出错了。 Currently while running the app it is throwing the error listed above.目前,在运行应用程序时,它会抛出上面列出的错误。

You're correct that the error is because the compiled SQLite module is named for Node by default;您是正确的,错误是因为编译的 SQLite 模块默认为 Node 命名; and thus Electron can't find it.因此 Electron 找不到它。 Using node-gyp to rebuild with the correct name, as in the answer above should work;使用node-gyp以正确的名称重建,如上面答案应该工作; but it's a lot easier to use the npm package electron-builder which will give you a better workflow and work with multiple platforms and versions without having to add scripts for node-gyp .但是使用 npm 包electron-builder要容易得多,这将为您提供更好的工作流程并使用多个平台和版本,而无需为node-gyp添加脚本。

See this answer to a similar question which details how to get it working.请参阅对类似问题的回答,其中详细介绍了如何使其工作。

Try rebuilding the sqlite3 package as follows:尝试按如下方式重建 sqlite3 包:

cd node_modules/sqlite3
npm run prepublish
node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.2-linux-x64
node-gyp rebuild --target=0.37.2 --arch=x64 --target_platform=linux --dist-url=https://atom.io/download/atom-shell --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.2-linux-x64

Check what is you electron version检查你是什么电子版

electron -v

Replace --target value with the vesion of your electron package.用您的电子包的版本替换 --target 值。

I tried running this with the latest Node version (6.2.2), if you are using an older version eg.如果您使用的是旧版本,例如,我尝试使用最新的 Node 版本 (6.2.2) 运行它。 the 4.2 then you will get an older bundle instead of the v48 your electron will need: 4.2 那么你会得到一个旧的包,而不是你的电子需要的 v48:

npm install sqlite3 --build-from-source

And simply renamed the folder in node_modules/sqlite3/lib/binding from node-v48-linux-x64 to electron-v1.2-linux-x64.并简单地将 node_modules/sqlite3/lib/binding 中的文件夹从 node-v48-linux-x64 重命名为 electron-v1.2-linux-x64。

It started complaining about requiring callbacks were they are supposed to be optional, but I just added them like this and it worked:它开始抱怨要求回调是否应该是可选的,但我只是像这样添加它们并且它起作用了:

var stmt = db.prepare(
  "INSERT INTO discount VALUES (?, ?, ?)",
  [1,2,3],
  () => true //Expected callback that is supposed to be optional
);

I just needed the Sqlite in my electron app for simple purposes and didn't have any other problems in my implementation.我只是为了简单的目的在我的电子应用程序中需要 Sqlite,并且在我的实现中没有任何其他问题。

First add new postinstall script in your package.json首先在 package.json 中添加新的 postinstall 脚本

"scripts": { "postinstall": "install-app-deps" }

Then install it using : npm install --save-dev electron-builder npm install --save sqlite3 npm run postinstall然后使用以下命令安装它: npm install --save-dev electron-builder npm install --save sqlite3 npm run postinstall

You can also find the same at : Electron application SQLITE package has not been found installed您也可以在以下位置找到相同的信息: 尚未找到安装 Electron 应用程序 SQLITE 包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM