How MySQL statements are spelled in node. js

Source: Internet
Author: User
Tags array length

Summarize the various ways MySQL statements are written in node. JS, referring to the example given by the NPM Web site mysql module .

  Query Select

1db.query (' select * from tuanshang_users where user_id < ', function (err,results,fields) {//if (err) throw err;    Console.log (results); if (!!    Results.length) {console.log (' query to ' + results.length + ' data ');    }else{Console.log (' No related data ');    }});//2db.query (' select * from Tuanshang_users where user_id=? ', [8],function (Err,results,fields) {if (err) throw err;  Console.log (results); });//3db.query ({sql: ' select * from Tuanshang_users where user_id=? and username=? ', values: [+, ' ABCD '],timeout:40000},    function (Err,results,fields) {if (err) throw err;  Console.log (results); });//4db.query ({sql: ' select * from Tuanshang_users where user_id=? and username=? ', timeout:40000},[44, ' ABCDEF '],    function (Err,results,fields) {if (err) throw err;  Console.log (results); });//5var userid = 44;var columns = [' username ', ' password '];var sql = ' Select?? From?? where user_id=? '; Db.query (sql,[columns, ' tuanshang_users ', userid],function (err,results,fields) {if (err) throw Err  Console.log (results); });

  inserting INSERT INTO

1var o = {username: ' Cnode ', Password: ' 123456 '};d b.query (' INSERT into tuanshang_users set? ', O, function (err,result) { C1/>console.log (result);    if (result) {     Console.log (' Insert succeeded ');     Console.log (Result.insertid);    } else{     Console.log (' Insert failed ');    }  ); /2db.query (' INSERT into tuanshang_users set? ', {username: ' Hello ', password: ' 123456 '}, function (Err,result) {    //if (err) throw err;    if (result) {     Console.log (' Insert succeeded ');     Console.log (result);    } else{     Console.log (' Insert failed ');    }  ); /3db.query ({sql: ' INSERT into tuanshang_users set? ', Values: {username: ' Kankan ', Password: ' 123456 '},timeout:40000}, function (err,result) {    //if (err) throw err;    Console.log (result);     if (!! Result) {     Console.log (' Insert succeeded ');     Console.log (Result.insertid);    } else{     Console.log (' Insert failed ');    }   ); /4 5 Reference Query Select

  Updating update

//1db.query (' Update tuanshang_users set? Where User_id=? ', [{username: ' Hello '},8],function (    Err,result) {//if (err) throw err;    Console.log (result); if (!! Result &&!!     Result.changedrows) {console.log (' update succeeded '); Console.log (result.changedrows);    }else{console.log (' update failed '); }});//2db.query (' Update tuanshang_users set? Where User_id=5 ', {username: ' Hello '},function (err,result) {//if (err) THR    ow err;    Console.log (result); if (!! Result &&!!    Result.changedrows) {console.log (' update succeeded ');     Console.log (result.changedrows);    }else{console.log (' update failed '); }});//3db.query ({sql: ' Update tuanshang_users set? Where user_id=? ', values: [{username: ' Hello '},8],timeout:40000},    function (Err,result) {//if (err) throw err;     Console.log (result); if (!! Result &&!!     Result.changedrows) {console.log (' update succeeded '); Console.log (result.changedrows);    }else{console.log (' update failed '); }});//4 5 Reference Query Select 

  Remove Delete  

1db.query (' Delete from tuanshang_users where username= ' Hello ', function (err,result) {    //if (err) throw err;    Console.log (result);     if (!! Result &&!! Result.affectedrows) {    console.log (' delete succeeded '); Console.log (result.affectedrows);     } else{    console.log (' delete failed ');    }  ); /2db.query (' Delete from tuanshang_users where user_id=? ', [10],function (Err,result) {    //if (err) throw err;    Console.log (result);     if (!! Result &&!! Result.affectedrows) {    console.log (' delete succeeded '); Console.log (result.affectedrows);     } else{    console.log (' delete failed ');    }  ); /3db.query ({sql: ' delete from tuanshang_users where user_id=? ', Values: [51],timeout:40000},function (Err,result) {    //if (err) throw err;    Console.log (result);     if (!! Result &&!! Result.affectedrows) {    console.log (' delete succeeded '); Console.log (result.affectedrows);     } else{    console.log (' delete failed ');}    ); /4 5 Reference Query Select

query operation, a result array is returned. If there is no query data, the result array is empty, that is, the array length is 0.

   Insert, UPDATE, delete operation returns a result object

{fieldcount:0,affectedrows:0,insertid:0,serverstatus:2,warningcount:0,message: ', protocol41:true,changedrows:0 }

After the insert operation, the result object Insertid Returns the location of the inserted data in the table ID.

After the update operation, changedrows in the resulting object returns how many rows were updated (the values of the fields in several data in the table change),affectedrows returns the rows that meet the Where condition effect.

After the delete operation, affectedrows in the resulting object returns how many rows were deleted. If 0 indicates that no row was deleted or the data row was not satisfied with the delete condition.

How MySQL statements are spelled in node. js

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.