Nodejs node-sqlite3 run callback not working

I am trying to perform a delete of a row in sqlite db using nodejs and node-sqlite3 package.

When I run the delete command, and manually check the entries, I can see that the query successfully deleted that row but I cant seem to write the code that confirms this.

This is the query

db.run("DELETE FROM Table1 WHERE id=? AND username=?", [id, user], function(error) {
console.log(error);
});

Regardless of a wrong or right input, it outputs null to the console. If the right details are given, it deletes it and prints null, if wrong id and user are given, it still prints null.

Any ideas on what might be wrong?

Thanks

There is nothing wrong in the node and node-sqlite3 behaviour here. Here are two parts to explain first regarding node and other regarding Sqlite.

Node

Your callback is getting called after execution of the statement. So nothing wrong here, since your callback is getting called (as proved by 'null' as output).

Sqlite

Delete query in Sqlite deletes if condition given in where clause evaluates to true, otherwise nothing is deleted.

Referring from node-sqlite3 documentation's Database#run api:

callback (optional): If given, it will be called when an error occurs during any step of the statement preparation or execution, and after the query was run. If an error occurred, the first (and only) parameter will be an error object containing the error message. If execution was successful, the first parameter is null.

So, in your case query execution succeeds without any error, resulting in error argument to callback function null as you see in output.

Further, if you want to check if any row was actually removed, you can use changes property as mentioned in the documentation:

If execution was successful, it contains two properties named "lastID" and "changes" which contain the value of the last inserted row ID and the number of rows affected by this query respectively. Note that "lastID" only contains valid information when the query was a successfully completed INSERT statement and "changes" only contains valid information when the query was a successfully completed UPDATE or DELETE statement. In all other cases, the content of these properties is inaccurate and should not be used. The .run() function is the only query method that sets these two values; all other query methods such as .all() or .get() don't retrieve these values.

Hope it helps...

I had similar problem, callbacks just would not fire. Period. The problem was that elsewhere I was calling process.exit(1), so my code was exiting before the the callbacks had a chance to return.

Search for process.exit, that may (or may not) save you hours of debugging and blaming sqlite :)

Off the topic: What bugs my mind is why they all-cap ID in lastID. It's not like it's an abbreviation like SQL or USA. It stands for Identification, which is one word.