[英]How to use sqlite3 module with electron?
I want to develop desktop app using electron that uses sqlite3 package installed via npm with the command我想使用电子开发桌面应用程序,该应用程序使用通过 npm 安装的 sqlite3 包和命令
npm install --save sqlite3
but it gives the following error in electron browser console但它在电子浏览器控制台中出现以下错误
Uncaught Error: Cannot find module 'E:\allcode\eapp\node_modules\sqlite3\lib\binding\node-v45-win32-x64\node_sqlite3.node'
My development environment is windows 8.1 x64 node version 12.7我的开发环境是windows 8.1 x64 node version 12.7
my package.json file looks like this:我的package.json文件如下所示:
{
"name": "eapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "electron ."
},
"author": "",
"license": "ISC",
"devDependencies": {
"electron-prebuilt": "^0.32.1"
},
"dependencies": {
"angular": "^1.3.5",
"sqlite3": "^3.1.0"
}
}
index.js file index.js 文件
var app = require('app');
var BrowserWindow = require('browser-window');
require('crash-reporter').start();
var mainWindow = null;
app.on('window-all-closed', function() {
if (process.platform != 'darwin') {
app.quit();
}
});
app.on('ready', function() {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600});
mainWindow.loadUrl('file://' + __dirname + '/index.html');
mainWindow.openDevTools();
mainWindow.on('closed', function() {
mainWindow = null;
});
});
my.js file my.js 文件
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('mydb.db');
db.serialize(function() {
db.run("CREATE TABLE if not exists lorem (info TEXT)");
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
console.log(row.id + ": " + row.info);
});
});
db.close();
index.html file index.html 文件
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div >
<div>
<h2>Hello</h2>
</div>
</div>
<!--<script src="js/jquery-1.11.3.min.js"></script>-->
<script src="js/my.js"></script>
</body>
</html>
By far the easiest way to use SQLite with electron is with electron-builder
.到目前为止,将 SQLite 与电子一起使用的最简单方法是使用
electron-builder
。
First, add a postinstall step in your package.json:首先,在 package.json 中添加安装后步骤:
"scripts": {
"postinstall": "install-app-deps"
...
}
and then install the necessary dependencies and build:然后安装必要的依赖项并构建:
npm install --save-dev electron-builder
npm install --save sqlite3
npm run postinstall
electron-builder will build the native module for your platform, with the correct name for the Electron binding;电子构建器将为您的平台构建本机模块,并为电子绑定提供正确的名称; and you can then
require
it in code as normal.然后你可以像往常一样在代码中
require
它。
See my github repo and blog post - it took me quite a while to figure this out too.请参阅我的github 存储库和博客文章- 我也花了很长时间才弄清楚这一点。
I would not recommend the native node sqlite3 module.我不会推荐本机节点 sqlite3 模块。 It requires being rebuild to work with electron.
它需要重建才能与电子一起使用。 This is a massive pain to do - At least I can never get it to work and their a no instructions to for rebuilding modules on windows.
这是一个巨大的痛苦 - 至少我永远无法让它工作,而且他们没有关于在 Windows 上重建模块的说明。
Instead have a look at kripken's 'sql.js' module which is sqlite3 that has been compiled 100% in JavaScript.而是查看 kripken 的“sql.js”模块,它是在 JavaScript 中 100% 编译的 sqlite3。 https://github.com/kripken/sql.js/
https://github.com/kripken/sql.js/
Two aspects are to be considered here:这里要考虑两个方面:
NODE_PATH
: this lets electron know where to find your modules (see this answer for a thorough explanation)NODE_PATH
:这让电子知道在哪里可以找到您的模块(有关详细解释,请参阅此答案)And checkout the following questions, that ask the same thing:并检查以下问题,这些问题提出了同样的问题:
My tip would be to give lovefield (by Google) a try.我的建议是尝试lovefield (by Google)。
I was having same problem.我遇到了同样的问题。 Tried everything and atlast this worked for me :-
尝试了一切,最后这对我有用:-
npm install --save sqlite3
npm install --save electron-rebuild
npm install --save electron-prebuilt
.\node_modules\.bin\electron-rebuild.cmd
This will create "electron-v1.3-win32-x64" folder in .\\node_modules\\sqlite3\\lib\\binding\\ location which is used by electron to use sqlite3.这将在 .\\node_modules\\sqlite3\\lib\\binding\\ 位置创建“electron-v1.3-win32-x64”文件夹,电子使用它来使用 sqlite3。
Just start application and you will be able to use sqlite3 now.只需启动应用程序,您现在就可以使用 sqlite3。
A simpler solution:一个更简单的解决方案:
npm i electron-rebuild --save-dev
npm i electron-rebuild --save-dev
./node_modules/.bin/electron-rebuild
(or .\\node_modules\\.bin\\electron-rebuild.cmd
on windows)./node_modules/.bin/electron-rebuild
(或.\\node_modules\\.bin\\electron-rebuild.cmd
在 Windows 上) PS: v47 is my version, be careful to choose the good one (in your case v45 ) PS: v47是我的版本,小心选择好的(你的情况是v45 )
npm install --save sqlite3
npm install --save-dev electron-rebuild
Then, in the scripts of your package.json, add this line:然后,在 package.json 的脚本中,添加以下行:
"scripts": {
"postinstall": "electron-rebuild",
...
},
Then just re-install to trigger the post-install:然后只需重新安装即可触发安装后:
npm install
Works flawlessly for me in a complex use case also involving electron-builder, electron-webpack and sequelize.在一个复杂的用例中对我来说完美无缺,还涉及电子构建器、电子网络包和续集。
It works in electron-webpack's dev mode and in production mode for both Windows and Linux.它适用于 electron-webpack 的开发模式和 Windows 和 Linux 的生产模式。
I encounter this error too.我也遇到这个错误。 Here is how i solve it:
npm install --save-dev electron-rebuild
then: ./node_modules/.bin/electron-rebuild
这是我解决它的方法:
npm install --save-dev electron-rebuild
然后: ./node_modules/.bin/electron-rebuild
from: https://electronjs.org/docs/tutorial/using-native-node-modules来自: https : //electronjs.org/docs/tutorial/using-native-node-modules
ps: While it's on rebuilding, don't use npm start
to lanch the electron app. ps:在重建过程中,不要使用
npm start
来npm start
电子应用程序。 Otherwise the rebuild process would fail.否则重建过程将失败。
它适用于版本 3 和 4,不幸的是版本 5。有关详细信息,请参阅 sqlite3 文档: https : //www.npmjs.com/package/sqlite3#custom-builds-and-electron或以其他方式运行以下行: npm install sqlite3 --runtime=electron --target=4.0.0 --dist-url=https://atom.io/download/electron
Have a look at a similar answer here看看这里的类似答案
TL;DR TL; 博士
cd .\node_modules\sqlite3
npm install nan --save
npm run prepublish
node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.3-win32-x64
node-gyp rebuild --target=1.3.2 --arch=x64 --target_platform=win32 --dist-url=http://electron.atom.io/ --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.3-win32-x64
You can manually build the native modules using visual studio.您可以使用 Visual Studio 手动构建本机模块。
In package.json create a script.在 package.json 中创建一个脚本。 "scripts": { "postinstall": "install-app-deps" }
“脚本”:{“安装后”:“安装应用程序”}
then run the script.然后运行脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.