Nodejs uses the mysql module to obtain the number of affected rows updated and deleted

Source: Internet
Author: User

In mysql, row_count () is used to directly make such a judgment. This statement must be followed by the SQL statement you run. node. js's I/o operations are asynchronous, which leads to a problem. It is not good to judge which SQL statement row_count () is executed. after a rough glance at the document, the document does not describe the problem. if you want to implement the synchronization through function nesting, you have no intention of finding that the affectedRows field is included in the parameters of the asynchronous function corresponding to SQL Execution. After testing, this is the result of row_count.
Instance:Copy codeThe 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': 'failed ',
'Msg ': msg,
'Data': null
});
Return;
}

Res. send ({
'Code': 200,
'State': 'success ',
'Msg ': 'updated ',
'Data': null
});
});

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.