Node-sqlite3: Unable to create sqlite file in different directory.

Created on 13 Aug 2015  路  5Comments  路  Source: mapbox/node-sqlite3

First off, thanks for the great work. I'm loving the library. My issue is that I have a db.js file in the root of my project, and a /data/ folder where I want to generate my sqlite databases.

This Works

If I run the following, the file gets created and opened just fine.

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('dev.sqlite');

This Does Not Work

If I run this though, the file is not created, and an error is logged. If I already have the file in data/dev.sqlite then it works as expected. The file is opened.

It seems like it should be creating the new database, even if a different directory is specified. It does not.

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./data/dev.sqlite');

Error:

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: SQLITE_CANTOPEN: unable to open database file
    at Error (native)

Any ideas?

Is there a separate method I'm missing to create a new database? Thanks!

Most helpful comment

Ah. The folder must exist! If I delete the data/ folder, the call fails (unable to create/open db). If I create an empty data/ folder, the sqlite file is created and opened.

Hopefully this helps someone in the future!

All 5 comments

Hmmm, now it's working. Sorry about that. I have no idea what I changed (considering it's only two line).

Ah. The folder must exist! If I delete the data/ folder, the call fails (unable to create/open db). If I create an empty data/ folder, the sqlite file is created and opened.

Hopefully this helps someone in the future!

Yes, Thanks.

OMG Thank you! that was the issue

I'm still getting error with creating sqlite file in different directory using
var db = new sqlite3.Database('../data/database.sqlite', yourCallback);

To fix it, I used these tips :

  1. Make sure that the folder exists
  2. Give the full path to the creation function like this :
    var db = new sqlite3.Database('C:/Users/You/Desktop/project/src/data/database.sqlite', yourCallback);
Was this page helpful?
0 / 5 - 0 ratings