How to operate MySQL data with Nodejs, in fact, the writing is still simple,
1. Start npm install MySQL--save in your node project
2. Introducing code into your new project
// Introducing a database var mysql=require (' MySQL '); // Implementing Local Links var connection = mysql.createconnection ({ ' localhost ', ' YF ', ' 123456 ', ' YF '})
The best thing to do is to use root to create conflicts.
3. Then the additions and deletions are checked, with the code attached
Inquire
// find function select () {connection.connect ( function if (err) {console.error ( ' Error connecting: ' + Err.stack) } console.log ( ' connected as ID ' + Connection.threadid); }) Connection.query ( ' SELECT * from demo ', function if (Error) throw error; Console.log ( ' The solution is: '
Add
// Add function Add () { = { 1, ' Hello MySql ', time:Date.now (), ' Deom ' }; function (Error, results, fields) { ifthrow error; }) // INSERT into posts ' id ' =1, ' title ' = ' Hello MySQL '}
Modify
//Modifyfunctionupdeate () {Connection.connect (function(err) {if(Err) {Console.error (' Error connecting: ' +err.stack); } console.log (' Connected as ID ' +Connection.threadid); }); Connection.query (' Update demo SET name=?where ID? ', [' Update ', 1],function(Error, results, fields) {if(Error)Throwerror; Console.log (' changed: ' + results.changerows + ' rows '); }); Connection.end ();}
Delete
//DeletefunctionDeletes () {Connection.connect (function(err) {if(Err) {Console.error (' Error connecting: ' +err.stack); return; } connection.query (' DELETE from demo SET where id=? ', [1],function(Error, results, fields) {if(Error)Throwerror; Console.log (' Deleted: ' + results.affectedrows + ' rows '); }); Console.log (' Connected as ID ' +Connection.threadid); Connection.end (); });}
Isn't it simple? Just add the method name and the corresponding parameters where you need it.
Nodejs working with MySQL database