node.js 如何在电子中使用 sqlite3 模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32504307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:17:12  来源:igfitidea点击:

How to use sqlite3 module with electron?

node.jssqlitenpmelectron

提问by manas

I want to develop desktop app using electronthat 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.jsonfile 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 Steve Melia

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 requireit in code as normal.

电子构建器将为您的平台构建本机模块,并为电子绑定提供正确的名称;然后你可以require像往常一样在代码中使用它。

See my github repoand blog post- it took me quite a while to figure this out too.

请参阅我的github 存储库博客文章- 我也花了很长时间才弄清楚这一点。

回答by Joue Bien

I would not recommend the native node sqlite3 module. 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.

我不会推荐本机节点 sqlite3 模块。它需要重建才能与电子一起使用。这是一个巨大的痛苦 - 至少我永远无法让它工作,而且他们没有关于在 Windows 上重建模块的说明。

Instead have a look at kripken's 'sql.js' module which is sqlite3 that has been compiled 100% in JavaScript. https://github.com/kripken/sql.js/

而是查看 kripken 的“sql.js”模块,它是在 JavaScript 中 100% 编译的 sqlite3。 https://github.com/kripken/sql.js/

回答by Yan Foto

Two aspects are to be considered here:

这里要考虑两个方面:

  1. Setting NODE_PATH: this lets electron know where to find your modules (see this answerfor a thorough explanation)
  2. Compiling native modules against electron headers: see official docs
  1. 设置NODE_PATH:这让电子知道在哪里可以找到您的模块(有关详细解释,请参阅此答案
  2. 针对电子头文件编译本机模块:请参阅官方文档

And checkout the following questions, that ask the same thing:

并检查以下问题,这些问题提出了同样的问题:



My tipwould be to give lovefield(by Google) a try.

我的建议是尝试lovefield(by Google)。

回答by Rj-s

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。

回答by Fabien Sa

A simpler solution:

一个更简单的解决方案:

  1. Install electron-rebuild npm i electron-rebuild --save-dev
  2. Launch electron-rebuild ./node_modules/.bin/electron-rebuild(or .\node_modules\.bin\electron-rebuild.cmdon windows)
  3. Go to "node_modules/sqlite3/lib/binding/" and rename the folder "electron-v0.36-darwin-x64" to "node-v47-darwin-x64"
  1. 安装电子重建 npm i electron-rebuild --save-dev
  2. 启动电子重建./node_modules/.bin/electron-rebuild(或.\node_modules\.bin\electron-rebuild.cmd在 Windows 上)
  3. 转到“ node_modules/sqlite3/lib/binding/”并将文件夹“ electron-v0.36-darwin-x64”重命名为“ node- v47-darwin-x64

PS: v47is my version, be careful to choose the good one (in your case v45)

PS:v47是我的版本,小心选择好的(你的情况是v45

回答by valleygtc

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 startto lanch the electron app. Otherwise the rebuild process would fail.

ps:在重建时,不要npm start用来启动电子应用程序。否则重建过程将失败。

回答by Sailab Rahi

It works for me in version 3 and 4, unfortunately NOT version 5. See the sqlite3 documentation for details: https://www.npmjs.com/package/sqlite3#custom-builds-and-electronor otherwise run the following line: npm install sqlite3 --runtime=electron --target=4.0.0 --dist-url=https://atom.io/download/electron

它适用于版本 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

回答by smkndblvr

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

回答by nicolas-van

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 的生产模式。

回答by Joel

You can manually build the native modules using visual studio.

您可以使用 Visual Studio 手动构建本机模块。

  1. Download visual studio 2019.
  2. Install package "desktop development with c++". In installation details tab select "MSVC v140 - VS 2015 C++ build tools (v14.00)"
  3. Download electron-builder in your project.
  4. In package.json create a script. "scripts": { "postinstall": "install-app-deps" }

  5. then run the script.

  1. 下载 Visual Studio 2019。
  2. 安装包“使用 c++ 进行桌面开发”。在安装详细信息选项卡中选择“MSVC v140 - VS 2015 C++ 构建工具(v14.00)”
  3. 在您的项目中下载电子生成器。
  4. 在 package.json 中创建一个脚本。“脚本”:{“安装后”:“安装应用程序”}

  5. 然后运行脚本。