Node.js MySQL UPDATE Table Query - Examples - Tutorial Kart

Node.js MySQL UPDATE Table Query ? Examples

Node.js MySQL UPDATE Table Query

You can update records of a table using MySQL UPDATE Query.

MySQL Update record(s) of a table

MySQL Update all records of a table

Node.js MySQL Update record(s) of a table

Consider the case that due to manual error, records in the students table are inserted with marks as 74 instead of 84. Now we shall execute a MySQL Update Query that updatesmarks column with value 84 where there is a value of 74.

UpdateRecordsFiltered.js - Update records of MySQL Table

// include mysql module var mysql = require('mysql');

//// cinrecalutdeeamcyosnqnlemcotidounle variable with the rveaqruimreydsqdle=tarielsquire('mysql'); var con = mysql.c reat eConnec t io n/(/{create a connection variable with the required details ipvhaaordscdto:ren"slo=scomaflyhssoeqsrlt.v"ce,rre//at eConnec t ion({ runhnoisntg: m"loycsaqllhost", // ip address of server running mysql

user: "arjun", // user naumseert:o"ayrojuunr"m, y//squlser name to your mysql database daptaasbsawseord: "password", // corresponding password

password: "padsastwaboardse",: /"/studentsDB" // use the specified database c}o)r;responding password

database: "studentsDB" // use t/h/emsapkeecitfoiedcodnanteacbtaiosen to the database. }c)o;n.connect(function(err) {

//ifm(aekrer)ttohcroownneercr;tion to/t/hief cdoantnaebcatsieo.n is successful c on.c onnec t (func t ion( errc)o{n.query("UPDATE students SET marks=84 WHERE marks=74", function (err, result, fields) {

i/f/(/ie/f rcirf)oantnhnyreocewtrirooenrrriw;shile executing above query, throw error succife(sesrfru)lthrow err;

c on.query("UPDAT E stud/e/ niftsthSeErTe is no error, you have the result marckso=n8so4leW.lHogE(RrEesult); marks=74", function (e}rr),;result, fields) { e}/x/)e;icfuatninygearrboorvwe hqilueery, throw error

if (err) throw err; // if there is no error,

yoRuuhnavtheethaeborevseulpt rogram in Terminal

c onsole.log(result ); Term})in;al Output

});

arjun@arjunVPCEH26EN:~/workspa ce/nodejs$ node Updat eRec ordsFilt ered. jasrjun@arjun-VPCEH26EN:~/workspace/nodejs$ node UpdateRecordsFiltered.js OkPacket { OfkiePladcCkoeutnt{: 0,

affiefeldcCtoeudnRto:w0s,: 3, insertId: 0, saefrfveecrtSetdaRtouws:s:343, wminaesrsensriatnIggdeC::o0u'(,nRto:w0s, mastecrhvedrS:t3atuCsh:a3n4g,ed: 3 Warnings: 0', pwroatroncinogl4C1o:untrtu:e0,, cmheasnsgaegdeR:o'w(Rso:w3s}matched: 3 Changed: 3 Warnings: 0',

protocol41: true,

changedRows: 3 }

Node.js MySQL Update all records of a table

Consider a scenario where the exam conducted to the students is cancelled due to some reason, you want to

reconduct the exam. So, you would like to update the marks for all the students to 0. Following Node.js MySQL

UPDATE Query Example shows how to update all the records of a table.

UpdateRecordsAll.js - Update All Records in a Table

// include mysql module var mysql = require('mysql');

//// cinrecalutdeeamcyosnqnlemcotidounle variable with the rveaqruimreydsqdle=tarielsquire('mysql'); var con = mysql.c reat eConnec t io n/(/{create a connection variable with the required details ipvhaaordscdto:ren"slo=scomaflyhssoeqsrlt.v"ce,rre//at eConnec t ion({ runhnoisntg: m"loycsaqllhost", // ip address of server running mysql

user: "arjun", // user naumseert:o"ayrojuunr"m, y//squlser name to your mysql database daptaasbsawseord: "password", // corresponding password

password: "padsastwaboardse",: /"/studentsDB" // use the specified database c}o)r;responding password

database: "studentsDB" // use t/h/emsapkeecitfoiedcodnanteacbtaiosen to the database. }c)o;n.connect(function(err) {

//ifm(aekrer)ttohcroownneercr;tion to/t/hief cdoantnaebcatsieo.n is successful c on.c onnec t (func t ion( errc)o{n.query("UPDATE students SET marks=84", function (err, result, fields) {

i/f/(/ie/f rcirf)oantnhnyreocewtrirooenrrriw;shile executing above query, throw error succife(sesrfru)lthrow err;

c on.query("UPDAT E stud/e/ niftsthSeErTe is no error, you have the result marckso=n8so4l"e,.flougn(crteiosunlt); (err, result, fields) { //})if; any error while et}hx)re;ocwuteinrgroar bove query,

if (err) throw err; // if there is no error,

Observe that there is no W HERE clause in the Query because of which all the records are selected for update process.

Conclusion :

In this Node.js Tutorial ? Node.js MySQL ? UPDATE Table Query, we have learnt to UPDATE records based on a condition or UPDATE all the records of a table.

Node.js

Node.js Tutorial

Get Started W ith Node.js

Install Node.js Ubuntu Linux Install Node.js Windows Node.js - Basic Example Node.js - Command Line Arguments Node.js - Modules Node.js - Create a module Node.js - Add new functions to Module Node.js - Override functions of Module Node.js - Callback Function Node.js - forEach

E xp re s s .j s

Express.js Tutorial What is Express.js? Express.js Application Example Install Express.js Express.js Routes Express.js Middleware Express.js Router

Node.js Buffers

Node.js Buffer - Create, Write, Read Node.js Buffer - Length Node.js - Convert JSON to Buffer Node.js - Array to Buffer

Node.js HTTP

Node.js - Create HTTP Web Server Node.js - Redirect URL

Node.js MySQL

Node.js MySQL Node.js MySQL - Connect to MySQL Database Node.js MySQL - SELECT FROM Node.js MySQL - SELECT WHERE Node.js MySQL - ORDER BY Node.js MySQL - INSERT INTO Node.js MySQL - UPDATE Node.js MySQL - DELETE Node.js MySQL - Result Object

Node.js MongoDB

Node.js MongoDB Node.js - Connect to MongoDB Node.js - Create Database in MongoDB Node.js - Drop Database in MongoDB Node.js - Create Collection in MongoDB Node.js - Delete Collection in MongoDB Node.js - Insert Documents to MongoDB Collection MongoError: failed to connect to server

Node.js Mongoose

Node.js Mongoose Tutorial Node.js Mongoose - Installation Node.js Mongoose - Connect to MongoDB Node.js Mongoose - Define a Model Node.js Mongoose - Insert Single Document to MongoDB Node.js Mongoose - Insert Multiple Documents to MongoDB

Node.js URL

Node.js - Parse URL parameters

Node.js FS (File System)

Node FS Node FS - Read a File

Node FS - Create a File Node FS - Write to a File Node FS - Append to a File Node FS - Rename a File Node FS - Delete a File Node FS Extra - Copy a Folder

Node.js JSON

Node.js Parse JSON Node.js Write JSON Object to File

Node.js Error Handling

Node.js Try Catch

Node.js Examples

Node.js Examples Node.js - Handle Get Requests Node.js Example - Upload files to Node.js server

Useful Resources

Node.js Interview Questions How to Learn Programming

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download