Home >Web Front-end >JS Tutorial >Nodejs uses the mysql module to obtain the number of rows affected by updates and deletions_javascript skills

Nodejs uses the mysql module to obtain the number of rows affected by updates and deletions_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:55:141688browse

The way to directly make such a judgment in mysql is to use row_count(). This statement must follow the sql statement you execute. The I/O of Nodejs is asynchronous, so this creates a problem, which is not easy to judge. Row_count() is the result of which SQL statement is executed. After a cursory glance at the document, this issue is not described in the document. I originally wanted to nest functions to achieve synchronization effects, but accidentally found that in the asynchronous function corresponding to the SQL execution There is an affectedRows field in the parameters. After testing, this is the result of row_count().
Example:

Copy code The code is as follows:

var cmd = 'UPDATE users SET ' field ' = ' value ' WHERE id = ' userid;
console.log(cmd);
db. query(cmd, function(err, rows, fields){
var affectedRows = rows.affectedRows;
if(err || affectedRows){
var msg = 'update ' field ' error';
Logger.error(msg);

res.send({
'code': 500,
'state': 'failure',
'msg': msg,
'data': null
}) ;
return;
}

res.send({
'code': 200,
'state': 'success',
'msg': 'updated',
'data': null
});
});

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn