如何将sqlite3模块与电子一起使用? [英] How to use sqlite3 module with electron?

查看:131
本文介绍了如何将sqlite3模块与电子一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用电子开发台式机应用程序,该程序使用通过npm通过命令安装的sqlite3软件包

I want to develop desktop app using electron that uses sqlite3 package installed via npm with the command

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'

我的开发环境是Windows 8.1 x64 节点版本12.7

My development environment is windows 8.1 x64 node version 12.7

我的 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文件

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文件

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文件

<!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>

推荐答案

到目前为止,将SQLite与电子结合使用的最简单方法是使用electron-builder.

By far the easiest way to use SQLite with electron is with electron-builder.

首先,在您的package.json中添加一个安装后步骤:

First, add a postinstall step in your 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将为您的平台构建本机模块,并使用Electron绑定的正确名称;然后您就可以像往常一样在代码中require了.

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.

请参阅我的 github存储库

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

这篇关于如何将sqlite3模块与电子一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆