Work every morning have more than half an hour mornings, in addition to each of the tasks, the rest is to share, for just into the industry of the rookie to know nothing, looking for technology to learn, and then share, really tired, but also a progress it
Nodejs operates MySQL, most of it is in the blog park to learn someone else's code, without understanding very thoroughly, just to share
First: Configure the Nodej environment and related packages, this way is mainly the MYSQ package, via the command npm install MySQL installation
Second: MySQL database operation
Create Data: Test database
To create a table in this database:
Then you can write the code:
Introduction Package
var mysql = require ('mysql');
Create a connection
Create Connection var client = mysql.createconnection ({ User: ' root ', password: ' Root ',
Connect to MySQL
Client.connect (function (err) { if(err) { console.log (' Erroer'); } Console.log ('succeed');});
Which database is linked
var ' Test ' // db client.query ("" + database);
Inquire
//Enquiryvarsql ='SELECT * from Usertest'; client.query (SQL, function (err, results, fields) {if(err) {Throwerr; } if(results) {//Enquiry for(vari =0; i < results.length; i++) {Console.log ("This ID is"+results[i].uid); Console.log ("This name is"+results[i].uname); Console.log (" This is"+results[i].uage); Console.log ("This sex is"+results[i].usex); Console.log ("This Tel is"+Results[i].utel); } } });
Similarly: Insert, modify, delete, etc. can be done this way
But there are other ways to insert it.
'Set uid =?, uname =?, Uage =?, Usex =?, Utel =? ' , [3'sv',1,' 13897686543'
Turn off data connections
// Close Connection client.end (function (err) { if(err) { console.log ('erroer '); } Console.log ('succeed');});
The database is simple to operate, not the best
Nodejs Operation MySQL