is an example of Nodejs connection to MySQL and the method of adding, deleting, modifying, and checking operation. To share with you for reference, it is as follows:

, preparation of

nodejs tutorial, mostly using operation mongodb as an example. But mongodb has some limitations, which is said on the official website. I'm going to use MySQL because I have a little bit of experience. First, the research is based on the research. Node-mysql, the hottest MySQL driver under the current node. It's been used preliminarily, because this way of asynchronous callback is a lot of craters.

the project package name is mysql, the version is shown in 2.5.4

mysql@ code to explain below, have the following code at the beginning, in the description of

 var after mysql.createConnection (connection = {host:'127.0.0.1', user:'root', password:'root123', port:'3306', database:'my_news_test', 

}); what is the meaning of the

code is very straightforward, if you want to, can go to the above website. A configuration like host, user, and a MySQL database application should be clear, and please modify the corresponding parameters by yourself. The code behind, there is a table called node_use assumes that the database "my_news_test" in the table, there are 3 properties of

id
age

MySQL MySQL: age test version 5.5

two database, and insert the 5 records of

 Source Database: my_news_test SET FOREIGN_KEY_CHECKS=0; Table structure for -------------------------------- node_user the DROP TABLE IF EXISTS `node_user`; CREATE TABLE `node_user` (`id` int NOT NULL AUTO_INCREMENT (11), `name` varchar (30) DEFAULT NULL, `age` int (8) DEFAULT N ULL PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 Records of node_user INSERT --------------------------------; the INTO `node_user` VALUES ('1','admin','32'); INSERT INTO node_user`, VALUES ('2','dans88','45'); INSERT INTO `node_user` VALUES ('3',' three ','35'); INSERT ('4'VALUES `node_user` INTO'ABCDEF','88', INSERT); INTO `node_user` VALUES ('5', 'Li Xiaoer','65'); 

three, the first test environment

1, first need to install the nodejs MySQL

 D:Usermyappejs4> NPM install 

; MySQL mysql@2.5.4 node_modulesmysql - require-all@0.0.8 - bignumber.js@1.4.1 had just - readable-str Eam@1.1.13 (inherits@2.0.1, string_decoder@0.10.31, isarray@0,.0.1, core-util-is@1.0.1)

2, written in the interaction of nodejs and MySQL code

 //mysql.js / / the first installation of nodejs MySQL //npm install package MySQL / / write the interaction of nodejs and MySQL code var MySQL ('mysql') var = require; TEST_DATABASE ='my_news_test'; VAR = TEST_TABLE'node_user'; / / create a connection var client = mysql.createConnection (password:'root123', user: {'root'}); (client.connect); client.query (use + TEST_DATABASE); client.query ('SELECT * FROM'+TEST_TABLE, function selectCb (err, results, fields) {if (ERR) {throw err;} if (results) {for (VaR = I 0; I < results.length; i++) {console.log ("%dt% st%s", results[i].id, Results[i].name, results[i].age (client.end);}});}); 

3,

 D:Usermyappejs4> node operating results; mysqltest.js 1 admin 322 dans88 453 three 354 885 65 

></p>
<p>

Node.js four, with MySQL increasing delete, change, check the operation of

1,

 var MySQL = require 

('mysql'); VAR connection = mysql.createConnection ({host:'127.0.0.1', user:'root', password:'root123', port:'3306', database:'my_news_test', connection.connect}); (VaR); userAddSql ='INSERT INTO node_user (ID, name, age VALU) ES (0,?,? '); VAR userAddSql_Params = ['Wilson', 55]; add connection.query / / increase (userAddSql, userAddSql_Params, function (err, result) {if (ERR) {console.log ('[INSERT - ERROR] ', err.message); return;} console.log ('-------INSERT----------'); //console.log ('INSERT ID:'result.insertId); console.log ('INSERT ID:' result); console.log ('#######################');}); (connection.end);

 D:Usermyappejs4, run the following > node mysqltestadd.js -------INSERT---------- INSERT ID: {fieldCount: 0, affectedRows: 1, insertId: 6, serverStatus: 2, warningCount: 0, protocol41: true, message:', changedRows: 

 and

2

 var mysql, require ('mysql') = VaR; connection = mysql.createConnection ({host:'127.0.0.1', user:'root', password:'root123', port:'3306', database:'my_news_test', connection.connect}); (VaR); userModSql'UPDATE = node_user SET = name, age =? WHERE? Id = var userModSql_Params = ['Hello? '; World', 99,7]; / / up modified (userModSql, connection.query userModSql_Params, function (err, result) {if (ERR) {console.log ('[UPDATE - ERROR] ', err.message); return;} console.log ('----------UPDATE-------------'); console.log ('UPDATE affectedRows', result.affectedRows (console.log);'************** ****************');}); connection.end (

); The results were as follows:

 D:Usermyappejs4> running node mysqltest_up.js ----------UPDATE------------- UPDATE affectedRows; 

3, 1 ******************************

 VaR to check the operation of MySQL = require ('mysql'); VAR connection = mysql.createConnection ({host:'127.0.0.1', user:'root', password:'root123', port:'3306', database:'my_news_test', connection.connect}); (VaR); userGetSql ='SELECT FROM * node_user'; / / query connection.query (userGetSql, function (err, result) {if (ERR) {console.log ('[SELECT - ERROR]', err.message); return;} console.log ('---------------SELECT----------------'); console.log (result); console.log ('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$');}); connection.e Nd (

);

 D:Usermyappejs4> operating results are as follows; node mysqltest_query.js ---------------SELECT---------------- id: name:'admin'[{1} {32, age:, id: 2, name:'dans88', age:} {45, id: 3, name: 'three', age: 35}, {id: 4}, name:'ABCDEF', age: 88 {5, id:, name:' Li Xiaoer ', age: 65}, {id: 6, name:'Wilson', age: 

></p>
<p>

4,

 var delete MySQL = require ('mysql'); VAR connection = mysql.createConnection ({host:'127.0.0.1', user password:'root123':'root', port:,'3306', database:,'my_news_test', connection.conne);} CT (VaR); userDelSql ='DELETE FROM node_user WHERE id = 7'; / / connection.query (userDelSql, function (err, result) {if (ERR) {console.log ('[DELETE - ERROR]', err.message); return;} console.log ('-------------DELETE--------------'); console.log ('DELETE affectedRows', result.affectedRows; console.log ('& &); & & & & & & & & & & & & & & & ');}); (connection.end); the operation results are as follows D:Usermyappejs4> node mysqltest_del.js -------------DELETE-------------- DELETE affectedRows 1 & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & 

,

add, delete, modify and check operation should be completed.

hopes that this article will help you in the design of the nodejs program.


This concludes the body part