Seth
Seth

Reputation: 1373

Electron JS + SQLite database

I have an Electron JS application that uses a local sqlite database. The sqlite database is a file within the project folder structure (lib folder). The application works just fine during testing, but when I build the app on Mac, and launch the .dmg file, it gets an error saying it cannot find the .sqlite database.

Is there a way to get this to work with the sqlite database I have in my project? Or is there a simpler way to do local storage in an Electron app?

Thank you.

Upvotes: 0

Views: 2200

Answers (2)

Ravi Kandala
Ravi Kandala

Reputation: 51

You must add buildResources extraResources in package.json to grab the SQLite file in production.

"build": {
  "appId": "com.app.app",
  "productName": "Electron APP",
  "files": [
    "build/**/*"
  ],
  "directories": {
    "buildResources": "build"
  },  
  "extraResources": [
    {
      "from": "./db/",
      "to": "db/",
      "filter": [
        "**/*"
      ]
    }
  ],
},

Upvotes: 0

Seth
Seth

Reputation: 1373

I found out this can be accomplished by writing the SQLite file to the local user's directory:

const electron = require('electron');
const path = require('path');
const userDir = (electron.app || electron.remote.app).getPath('userData');
const dbPath = path.join(userDir, 'mydb.sqlite');

Upvotes: 1

Related Questions