I am trying to delete a record using an SQL Delete statement and trying to find any record with a 'conkey' of a specific value that is entered in a form.
My code:
var sqlQueryCommand = 'DELETE FROM `DATABASE`.`CON_LOOKUP` WHERE conkey=' + connection.escape(req.body.conkey);
applogger.info('sqlQueryCommand: ' + sqlQueryCommand);
connection2.connect(function(err){
if(err && jslog == 1){
logger.debug('Connection 2 Error:' + err);
}
});
connection2.query(sqlQueryCommand, function(match_err_QC, results)
{
// Check for error from SQL
if (match_err_QC) throw match_err_QC;
else{
var response_json = {
"app": appname,
"response_code": "1",
"conkey": connection.escape(req.body.conkey),
"response": "success"
};
var response_text = JSON.stringify(response_json);
response.writeHead(200, {
'Content-Length': response_text.length,
'Content-Type': 'text/plain' });
response.write(response_text);
response.end();
}
});
When I enter a 'conkey' that does not exist in the database, it does not throw an error.
I'm not sure if I should be checking for the record(s) first then deleting them, or just add a piece to my code.
Any help is appreciated. :)
DELETE statements that did not delete any rows will not generate an error. What you can do however is check results.affectedRows. If it's 0 then no rows were deleted.