Search code examples
node.jsrestsqlite

How to check whether update succeeded in node.js and sqlite3 ?


Could anyone give me any advice on how to check whether an update statement has succeeded when using node.js and the sqlite3 package installed from npm install sqlite3 (https://github.com/developmentseed/node-sqlite3/wiki/API)

To perform an update, I'm doing the following:

    var id = req.params.id;
    var thing = req.body;
    stmt = db.prepare("UPDATE FOO set name=?, year=? where id=?");
    stmt.bind(thing.name, thing.year, id);
    stmt.run(function(err, result) {
        if (err) {
            res.send(JSON.stringify(err));
            res.send({'error':'An error has occurred'});
        } else {
            if (result) {
                console.log('Success: ' + JSON.stringify(result));
            }
            res.send(JSON.stringify(wine));
        }
    });

However, result is never defined.

If I change stmt.run() to be stmt.all(), I get a result, but it seems to be empty - printing it to the console shows nothing.


Solution

  • The result is for the records returned from a SELECT query.

    If the UPDATE command fails, you get err.

    To find out how many records have been updates, see changes.