admin管理员组文章数量:1401509
I've had this problem for a while now and I can't seem to figure it out, I've looked around on Stackoverflow but could only find either PHP or C# code matching my problem. I'm trying to check if a row exist, if it doesn't then send a message that it does not, if it does send a message that it does.
Here's my code,
query('SELECT * FROM `cases` WHERE `case`='+pool.escape(String(getCase)), function(err, row) {
if(err) {
logger.error('Error in DB');
logger.debug(err);
return;
}else{
console.log('Case row was found!');
}
});
Also, getCase is a variable that I can change, but even when it's not in the database it seems to return Case row was found!
I've had this problem for a while now and I can't seem to figure it out, I've looked around on Stackoverflow but could only find either PHP or C# code matching my problem. I'm trying to check if a row exist, if it doesn't then send a message that it does not, if it does send a message that it does.
Here's my code,
query('SELECT * FROM `cases` WHERE `case`='+pool.escape(String(getCase)), function(err, row) {
if(err) {
logger.error('Error in DB');
logger.debug(err);
return;
}else{
console.log('Case row was found!');
}
});
Also, getCase is a variable that I can change, but even when it's not in the database it seems to return Case row was found!
-
What database engine are you using? And what library/module are you using for the
query()
function? – sadmicrowave Commented Apr 3, 2017 at 21:09 -
@sadmicrowave sorry for not specifying, I use
mysql
– Martijn Ebbens Commented Apr 3, 2017 at 21:13
2 Answers
Reset to default 3It is returning Case row was found!
because you are only telling the callback to stop if an error occurred. You need to extend the else
statement to check if any data is present in the row
variable like this:
query('SELECT * FROM `cases` WHERE `case`='+pool.escape(String(getCase)), function(err, row) {
if(err) {
logger.error('Error in DB');
logger.debug(err);
return;
} else {
if (row && row.length ) {
console.log('Case row was found!');
// do something with your row variable
} else {
console.log('No case row was found :( !');
}
}
});
The accepted answer does not work for me somehow. I found another solution here
Excerpt from the article:
You have a table address and want to check if there’s already a row with address_id = 100 existing. You can use COUNT to check:
SELECT COUNT(*) FROM address WHERE address_id = 100;
But if you are looking for a faster query with a yes/no answer:
SELECT EXISTS(SELECT 1 FROM address WHERE address_id = 100);
And I wanna add we also can use LIMIT
:
SELECT * FROM address WHERE address_id = 100 LIMIT 1;
本文标签: javascriptCheck if ROW already exists in Database using query gt NodeJSStack Overflow
版权声明:本文标题:javascript - Check if ROW already exists in Database using query > NodeJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744289407a2599042.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论