SQLITE_CANTOPEN: unable to open database file


#1

I am trying to run an app on my local machine but everytime I run it after packaging it throws me following error:

Uncaught Exception:
Error: SQLITE_CANTOPEN: unable to open database file
at Error (native)

If I run it through command line npm start then it works fine.

Here is my code:

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./todo.db');
var http = require('http');
var dispatcher = require('httpdispatcher');
const PORT=6200;

var create_todos = "CREATE TABLE IF NOT EXISTS todos (description TEXT, done BOOL);",
  get_todos = "SELECT rowid AS id, description, done FROM todos ORDER BY done ASC, id DESC",
  add_todo = "INSERT INTO todos (description, done) VALUES (?, 0)",
  delete_todo = "DELETE from todos WHERE rowid=?",
  toggle_todo = "UPDATE todos SET description=? WHERE rowid=?";

db.run(create_todos);

function handleRequest(request, response){
    try {
        //log the request on console
        console.log(request.url);
        //Disptach
        dispatcher.dispatch(request, response);
    } catch(err) {
        console.log(err);
    }
}

//Create a server
var server = http.createServer(handleRequest);

//Lets start our server
server.listen(PORT, function(){
    //Callback triggered when server is successfully listening. Hurray!
    console.log("Server listening on: http://localhost:%s", PORT);
});

dispatcher.onGet("/get", function(req, res) {
    var todos = [];
    db.each(get_todos,
      function(err, row) {
        todos.push({'id' : row.id, 'title' : row.description});
      }, function () {
        res.end(JSON.stringify(todos));
      }
    );
    res.writeHead('200', {'Content-Type': 'application/json'});
    res.end(todos);
}); 


dispatcher.onPost("/create", function(req, res) {
    const data = JSON.parse(req.body);
    db.run(add_todo, data.title);
    res.writeHead('200', {'Content-Type': 'text/plain'});
    res.end('true');
}); 

dispatcher.onPost("/update", function(req, res) {
    const data = JSON.parse(req.body);
    db.run(toggle_todo, data.title, data.id);
    res.writeHead('200', {'Content-Type': 'text/plain'});
    res.end('true');
}); 

dispatcher.onPost("/delete", function(req, res) {
    const data = JSON.parse(req.body);
    db.run(delete_todo, data.id);
    res.writeHead('200', {'Content-Type': 'text/plain'});
    res.end('true');
}); 

From error it looks like it is not able to find the sqllite db file, but I tried with various different paths. Same error. To generate setup file I am using following command:

electron-packager ./ todo --platform=darwin --arch=x64 --version=0.37.2


#2

What is this path relative to? Probably not what you think it’s relative to. Instead of leaving it to chance construct an absolute path to the database file so your app doesn’t break when the working directory changes after the app is packaged.


#3

So what should I keep the path, initially I had set it to var db = new sqlite3.Database('todo.db');
It failed


#4

You can use the path module and __dirname to get the full path to your database.

const path = require('path')
const dbPath = path.resolve(__dirname, 'todo.db')
const db = new sqlite3.Database(dbPath)

#5

Thanks,
this worked :slight_smile:


#6

Saved the Day :slight_smile: Ty