应用程序安装后未立即创建sqlite3 db文件 - react electron js

发布于 2025-01-31 12:25:17 字数 1197 浏览 5 评论 0原文

我是使用React和Electron JS使用SQLITE 3创建Windows应用程序。 IM将数据库文件保存在根文件夹中的“/数据库”文件夹中。我注意到的一件事是DATABASE.DB文件仅在我在DEV模式下运行'npm start'之后才能创建。

当我使用电子构建器构建应用程序时,该应用程序将无需任何问题而创建。问题是,一旦第一次安装了应用程序,除了数据库文件之外,创建了所有文件。安装后自动打开该应用程序。由于未创建数据库文件,因此会引发错误,例如“无法读取数据库”。关闭应用程序并通过打开.exe文件重新打开它后,创建数据库文件夹和文件。我需要将DB文件包装在构建的根文件夹中,而不是内部/构建/资源/电子的DIST文件夹。任何帮助将不胜感激。

是我的软件包。

"build": {
    "appId": "com.electron",
    "files": [
      "dist/electron.js",
      "dist/index.html",
      "dist/main.js",
      "dist/images",
      "dist/preload.js",
      "dist/**"
    ],
    "extraResources": [{
      "from": "./databases/",
      "to": "databases/",
      "filter": [
        "**/*"
      ]
    }],
    "directories": {
      "output": "./electron"
    },
    "electronDownload": {
      "cache": "./electron/cache"
    },
    "win": {
      "target": "msi",
      "icon": "./src/assets/images/icon.png"
    },
    "asar": false
  }

以下

const dbName = 'mydatabase.db';
const dbPath = path.join(path.dirname(__dirname), 'databases');
const fullDbPath = path.join(dbPath, dbName);

if(!fs.existsSync(dbPath)) {
  fs.mkdirSync(dbPath, { recursive: true });
}

I`m creating an windows app using React and Electron JS with database file using sqlite3. Im saving the database file in the '/databases' folder in the root folder. The one thing which I noticed is the database.db file is created only after I run 'npm start' in dev mode.

When I build the app using electron-builder, the app gets created without any issues. The problem is, once the app is installed for the first time, all the files are created except the db file. The app is opened automatically after installation. Since db file is not created, it throws error like 'Could not read database'. Once I close the app and reopen it by opening the .exe file, the database folder and the file are created. I need the DB file to be packed with the build in the root folder of the build not inside /build/resources/dist folder of electron. Any help would be appreciated.

Below is my package.json:

"build": {
    "appId": "com.electron",
    "files": [
      "dist/electron.js",
      "dist/index.html",
      "dist/main.js",
      "dist/images",
      "dist/preload.js",
      "dist/**"
    ],
    "extraResources": [{
      "from": "./databases/",
      "to": "databases/",
      "filter": [
        "**/*"
      ]
    }],
    "directories": {
      "output": "./electron"
    },
    "electronDownload": {
      "cache": "./electron/cache"
    },
    "win": {
      "target": "msi",
      "icon": "./src/assets/images/icon.png"
    },
    "asar": false
  }

Below is how I handle the database creation in electron.js:

const dbName = 'mydatabase.db';
const dbPath = path.join(path.dirname(__dirname), 'databases');
const fullDbPath = path.join(dbPath, dbName);

if(!fs.existsSync(dbPath)) {
  fs.mkdirSync(dbPath, { recursive: true });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠书 2025-02-07 12:25:17

构建电子应用时,我对.db文件有同样的问题。我尝试使用KNEX.JS处理它。 KNEX.JS GUID说,您必须为SQLIT3或这样更好的sqlit定义KNEX:

const knex = require('knex')({
  client: 'sqlite3', // or 'better-sqlite3'
  connection: {
    filename: "./mydb.sqlite"
  }
});

因此,您需要在公共文件夹中一个.sqlite文件,即使在构建项目之后,它也可以使用

I have same problem with .db files when I build my electron app. I tryed to using Knex.js to handle It. Knex.js guid says you have to define Knex for sqlit3 or better-sqlit like this:

const knex = require('knex')({
  client: 'sqlite3', // or 'better-sqlite3'
  connection: {
    filename: "./mydb.sqlite"
  }
});

so you need a .sqlite file in your public folder and It`s usable even after building your project

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文