Nodejs ways to get updates and delete affected rows using the MySQL module

Source: Internet
Author: User
Tags what sql

  spare time to play nodejs when you encounter a bit of egg pain, in the use of MySQL module connection MySQL operation, want to update, DELETE statement, want to know exactly update, delete succeeded no

The way to do this directly in MySQL is to use Row_count (), which is followed by the SQL statement you execute. and Nodejs I/O is asynchronous so this creates a problem that is not very good at judging what SQL Execution Row_count () is. A cursory glance at the document does not describe the problem. The function nested to achieve the effect of synchronization, but did not find that in the execution of the SQL corresponding asynchronous functions in the parameters in the  affectedrows field, tested, the product is the result of Row_count (). Instance: Copy code code 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 ': $,    ,     ' state ': ' Failure ', & nbsp       ' msg ': Msg,    ,     ' data ': null  ,    }       return;    }       Res.send ({   ,   ' code ':,    ,   ' 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.