Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 16:55:14
Original
1653 people have browsed it

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
});
});

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template