pkg icon indicating copy to clipboard operation
pkg copied to clipboard

SQLITE_CANTOPEN: unable to open database file

Open colinmcparland opened this issue 8 years ago • 18 comments

Node v8.3.0 NPM v5.3.0 [email protected]

package.json:

  "name": "cca",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "inquirer": "^4.0.1",
    "lowdb": "^1.0.0",
    "nexe": "^2.0.0-rc.22",
    "pkg": "^4.3.0-beta.1",
    "request": "^2.83.0",
    "request-promise-native": "^1.0.5",
    "sqlite3": "^3.1.13"
  },
  "description": "",
  "bin": "./main.js",
  "pkg": {
    "assets": "./main.db"
  }
}

Im using sqlite3 package for node, trying to access my db file:

function connectToDB() {
  return new Promise(function(resolve, reject) {
    var db_path = path.resolve(__dirname, 'main.db');
    console.log(db_path);
    test = fs.statSync(db_path);
    console.log(test);
    db = new sqlite3.Database(db_path, (err) => {
      if(err) {
        reject('Error: ' + err);
      }
      else {
        resolve('Conected to DB');
      }
    });
  })
}

I know the file is there as it can be reached by fs, here is the output from console.logs:

/snapshot/dist/main.db
{ mode: 33279,
  size: 8192,
  blksize: 4096,
  blocks: 0,
  dev: 0,
  gid: 20,
  ino: 0,
  nlink: 0,
  rdev: 0,
  uid: 500,
  atime: 2017-12-12T21:17:30.860Z,
  mtime: 2017-12-12T21:17:29.156Z,
  ctime: 2017-12-12T21:17:29.156Z,
  birthtime: 2017-12-12T21:17:29.156Z,
  atimeMs: 1513113450860,
  mtimeMs: 1513113449156,
  ctimeMs: 1513113449156,
  birthtimeMs: 1513113449156,
  isFile: [Function],
  isDirectory: [Function],
  isSymbolicLink: [Function],
  isFIFO: [Function] }

And the sqlite3 error:

Error: SQLITE_CANTOPEN: unable to open database file

colinmcparland avatar Dec 12 '17 21:12 colinmcparland

@colinmcparland how did you resolve the issue

edit: fixed - my issue was that i used path.resolve(__dirname, filename) to locate my db files

Bonuspunkt avatar Feb 01 '18 13:02 Bonuspunkt

meet the same issue, any one can help?

Jackie1Wang avatar Nov 09 '18 04:11 Jackie1Wang

had this for months, any solution?

stiofand avatar Jan 10 '19 12:01 stiofand

I'm also having this same issue. Is there a reason why it's closed?

Tried with and without path.resolve() (and path.normalize() etc.)... all get same same issue: file exists but sqlite via nodejs gets the SQLITE_CANTOPEN error as described here.

I've set READONLY on the db, since I'm pretty sure it won't be writable since code isn't in /tmp and underlying Lambda isn't writable.

Could very will be missing something, but tried a bunch.

shafdog avatar Jan 14 '19 23:01 shafdog

check your (resolved) path (ex. log it) mine pointed after packaging to a pkg internal path

also https://github.com/zeit/pkg#snapshot-filesystem

Bonuspunkt avatar Jan 14 '19 23:01 Bonuspunkt

I had this issue also and resolved it by creating directory "database" in my project directory.

JonYergin avatar Jul 31 '19 12:07 JonYergin

var db = new sqlite3.Database( path.resolve(__dirname, 'db.sqlite') );

riotom avatar Oct 15 '19 15:10 riotom

Hi folks! I'm also facing the same issue. Some times it works fine with my current configuration. This is my knex configuration.

module.exports = {

development: { client: 'sqlite3', connection: { filename: './src/database/db.sqlite' } }, ...

This is my connection file:

const knex = require('knex'); const configuration = require('../../knexfile'); const connection = knex(configuration.development); module.exports = connection;

The only way I found to solve the this issue is to delete the src/database/db.sqlite and create the db.sqlite again in the same path

sousadgaspar avatar Mar 28 '20 19:03 sousadgaspar

Hi folks!

Thankfully after a tone of research I found the issue. To solve we have to use the absolute path on the database path like the following script:

development: { client: 'sqlite3', connection: { filename: '/absolutepath/db.sqlite' }

sousadgaspar avatar Mar 28 '20 20:03 sousadgaspar

Thanks! was having the same problem. @sousadgaspar

macindex avatar Mar 29 '20 15:03 macindex

Cara muito obrigado @sousadgaspar

jairodias avatar May 12 '20 09:05 jairodias

filename: 'dir_name/db.sqlite', //just leave out the first /

julianjgcode avatar Sep 23 '21 10:09 julianjgcode

Maybe this means nothing, I don't know I'm a beginner at node. I ran a tutorial on http://localhost:3000/ which worked fine. Some while later tried torequire('sqlite3') in an existing project using http://localhost:8080/ and ended up here after staring at the same error.

Reopening the tutorial worked. The page says Please enter 'http://localhost:3000/close' to close the database connection before closing this window Enter that in Firefox devtools and suddenly I have working sqlite3 on 8080.

Node is running in WSL on Windows 11; the machine has been rebooted a dozen times in the interim. I can now run both servers in different browser tabs, so it doen't really add up but I'll leave this here just in case it helps someone.

perplexed99 avatar Apr 09 '22 08:04 perplexed99

@colinmcparland how did you resolve the issue

edit: fixed - my issue was that i used path.resolve(__dirname, filename) to locate my db files

Work for me

fabsalves avatar Apr 21 '22 21:04 fabsalves

how to resolve the error

Sonali2567 avatar May 15 '22 14:05 Sonali2567

I had the same issue. In my case i just needed to clean my database file because i already had a "database.sqlite" named folder. This error is related to problems on the folder structure / path

jrmsjorgesilva avatar Aug 02 '22 13:08 jrmsjorgesilva

For anyone having this issue, my problem was that i had in .env a variable called DATABASE_URL="./pathtodbfile"

Just changing double quotes for single quotes done the work.

DATABASE_URL="./pathtodbfile" >>> DATABASE_URL='./pathtodbfile'

Victor-Ross avatar Mar 16 '23 19:03 Victor-Ross

i was encountering the same issue .i created separate directory daatabase and removed sqlite.READ WRITE command.Modified code const db = new sqlite3.Database("database", (err) => { if (err) { console.error(err.message); } else { console.log('Connected to the SQLite database.'); } });issue resolved

Rabiyya1302 avatar Nov 27 '23 18:11 Rabiyya1302