Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

SQLITE_CANTOPEN: unable to open database file #304

Closed
colinmcparland opened this issue Dec 12, 2017 · 18 comments
Closed

SQLITE_CANTOPEN: unable to open database file #304

colinmcparland opened this issue Dec 12, 2017 · 18 comments

Comments

@colinmcparland
Copy link

colinmcparland commented Dec 12, 2017

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

@Bonuspunkt
Copy link

Bonuspunkt commented Feb 1, 2018

@colinmcparland how did you resolve the issue

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

@Jackie1Wang
Copy link

meet the same issue, any one can help?

@stiofand
Copy link

had this for months, any solution?

@shafdog
Copy link

shafdog commented Jan 14, 2019

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.

@Bonuspunkt
Copy link

Bonuspunkt commented Jan 14, 2019

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

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

@JonYergin
Copy link

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

@riotom
Copy link

riotom commented Oct 15, 2019

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

@sousadgaspar
Copy link

sousadgaspar commented Mar 28, 2020

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
Copy link

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'
}

@macindex
Copy link

Thanks! was having the same problem. @sousadgaspar

@jairodias
Copy link

Cara muito obrigado @sousadgaspar

@julianjgcode
Copy link

julianjgcode commented Sep 23, 2021

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

@perplexed99
Copy link

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.

@fabsalves
Copy link

@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

@Sonali2567
Copy link

how to resolve the error

@jrmsjorgesilva
Copy link

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

@Victor-Ross
Copy link

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'

@Rabiyya1302
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests