电子打包程序:打包后找不到本地db文件 [英] Electron-packager: can not locate local db file after packing
问题描述
背景:
在我的应用中,我有一个本地数据库文件,名为 example.db
,该文件由 main.js
。我的 main.js
程序的项目结构和部分如下所示。
Background:
In my app, I have a local DB file, named example.db
which is read by the main.js
. The project structure and part of my main.js
program is shown below.
项目文件夹体系结构
主要。 js
const {
app,
BrowserWindow,
dialog,
Menu
} = require('electron')
const fs = require('fs')
const path = require('path')
const sqlite3 = require('sqlite3').verbose()
// load dataBase
let dbFile = 'app/db/example.db'
const db = new sqlite3.Database(dbFile)
Package.json
{
"name": "Example",
"version": "1.0.0",
"description": "",
"main": "./app/main.js",
"scripts": {
"postinstall": "install-app-deps",
"start": "electron ."
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"electron-builder": "^15.5.1",
"electron-prebuilt": "^1.4",
"electron-rebuild": "^1.5.7",
"electron-packager": "^8.1.0"
},
"dependencies": {
"jquery": "^3.1.1",
"sqlite3": "^3.1.8"
}
}
问题:
我可以通过运行 npm start
来成功加载数据库文件。通过在 Example
文件夹下运行以下命令行,我成功地将程序打包到Windows平台:
I can load the DB file successfully by running the npm start
, however, after I successfully packing my program to Windows platform by running the following command line under Example
folder:
node_modules / .bin / electron-packager --platform = win32 --arch = x64。
我运行了 .exe
文件,但系统显示找不到我的数据库文件。
I ran the .exe
file, but the system shows that it can not find my Database file.
尝试过的方法:
我尝试使用 process.resourscesPath $ c $修改
main.js
中的db文件路径c>如下:
I tried to modify the db filepath in the main.js
by using process.resourscesPath
as the following:
// load dataBase
let dbFile = path.join(process.resourcesPath, '/app/db/example.db')
const db = new sqlite3.Database(dbFile)
但是,这样做之后,我既无法通过运行 .exe
加载数据库文件,也无法通过运行 npm start
。
However, after doing that, I neither can load the DB file from running .exe
, nor can I make it by running npm start
.
问题:
我想做什么是存储数据库文件的正确方法,以及如何在我的后端程序(在此示例中为 main.js
)中写入文件的路径。谢谢。
I want to what is the proper way to store my Database file, and how to write the path of it in my back-end program (main.js
in this example). Thanks.
推荐答案
运行 .exe
时,当前目录可能会会有所不同,因此相对路径将不正确。如您所知,您需要整理完整的路径,而不是 process.resourcesPath
,而应使用 app.getAppPath()
。
When running the .exe
, the current directory might be different so the relative path will not be correct. As you figured out you need to put together a full path, but rather than process.resourcesPath
you should use app.getAppPath()
.
let dbFile = path.join(app.getAppPath(), 'app', 'db', 'example.db')
这应该给出数据库文件的正确路径。您可能面临的另一个问题是 .exe
通常与 Asar 格式,它提供读访问权限,但不提供写访问权限。因此,如果您需要写入数据库,最好将其放置在其他位置。 electron.app.getPath(name) API,例如,您可以使用 app.getPath( userData)
来获取为每个用户的应用程序数据提供的路径。在这种情况下,您的应用程序必须在该位置创建数据库文件。
This should be give the correct path to the database file. An additional problem you may face is that the .exe
will typically be packaged with the Asar format, which gives read access but not write access. So if you need to write to the database, it may be better to place it elsewhere. There are a number of paths provided by the electron.app.getPath(name) API, so for example you could use app.getPath("userData")
to get the path provided for per-user application data. In this case your application would have to create the database file at that location.
这篇关于电子打包程序:打包后找不到本地db文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!