8

I've searched on how to create a sqlite3 database with a callback in Node.js and have not been able to find any links. Can someone point me towards documentation or provide a 2-3 line code sample to achieve the following:

  • Create a sqlite3 database and catch an error if the creation fails for any reason.

Here is what I've tried:

     let dbCreate = new sqlite3.Database("./user1.db", sqlite3.OPEN_CREATE, function(err){

        if(!err){
           logger.infoLog("Successfully created DB file: " + dbFileForUser + " for user: " + username );
        } else {
           logger.infoLog("Failed to create DB file: " + dbFileForUser + ". Error: " + err );
        }
     });

     dbHandler[username]  = dbCreate;

When I execute this, I get the following error: "Failed to create DB file: ./database/user1.db. Error: Error: SQLITE_MISUSE: bad parameter or other API misuse"

This call without callback works just fine.

var customDB = new sqlite3.Database("./custom.db", sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE);

But in this, I will not know if I run into any errors while creating the Database.

2 Answers 2

26

Try this:

let userDB = new sqlite3.Database("./user1.db", 
    sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, 
    (err) => { 
        // do your thing 
    });

Example.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the prompt response and link to what looks like excellent documentation. I did find the solution after I posted the question and your documentation has similar solution. I would love to give you credit for this since you provided the documentation, but your current answer has the same problem. The callback as ......, function(err) { // do your thing }); Here is the code that works: let userDB = new sqlite3.Database("./user1.db", sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => { //do your thing }); If you update your answer to reflect this, I'll accept it.
It worked, detailed information can be found at sqlitetutorial.net/sqlite-nodejs/connect.
I still get SQLITE_CANTOPEN: unable to open database file. what can l do?
I resolved the SQLITE_CANTOPEN issue by passing a file location without directory to the sqlite3.database. e.g let userDB = new sqlite3.Database("./**file-path-with-no- directory**.db", sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => { //do your thing });
1

@Irvin is correct, we can have a look at http://www.sqlitetutorial.net/sqlite-nodejs/connect/ and check it says if you skip the 2nd parameter, it takes default value as sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE and in this case if database does not exist new database will be created with connection.

sqlite3.OPEN_READWRITE: It is to open database connection and perform read and write operation.

sqlite3.OPEN_CREATE : It is to create database (if it does not exist) and open connection.

So here is the first way where you have to skip the 2nd parameter and close the problem without an extra effort.

const sqlite3 = require("sqlite3").verbose();

let db = new sqlite3.Database('./user1.db', (err) => {
    if (err) {
        console.error(err.message);
    } else {
        console.log('Connected to the chinook database.|');
    }
});


db.close((err) => {
    if (err) {
        return console.error(err.message);
    }
    console.log('Close the database connection.');
});

And this is the 2nd way to connect with database (already answered by @Irvin).

const sqlite3 = require("sqlite3").verbose();

let db = new sqlite3.Database('./user1.db', sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE
, (err) => {
    if (err) {
        console.error(err.message);
    } else {
        console.log('Connected to the chinook database.');
    }
});


db.close((err) => {
    if (err) {
        return console.error(err.message);
    }
    console.log('Close the database connection.');
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.