NodeJS operation MySQL1. create a table in the database (lzq in example) (teachers in example ). 2. Install the MySQL driver for nodeJS: npminstallmysql; 3. Run the startSQL. js file: nodestartSQL. js4. test environment: node. js0.... Synta
NodeJS for MySQL operations
1. Create a table (such as teachers) in the database (such as lzq ).
2. Install the MySQL driver for nodeJS:
Npm install mysql;
3. Execute the startSQL. js file:
Node startSQL. js
4. test environment:
Node. js 0.6.12
5. A total of two js files: startSQL. js and CRUD. js.
StartSQL. js --------------------------------->
01
Var CRUD = require ('./CRUD '),
02
Sys = require ('util '),
03
Client = require ('mysql'). createClient ({'host': 'localhost ',
04
& Apos; port: 3306,
05
'User': 'root ',
06
'Password': 'admin '}),
07
ClientConenctionReady = function (client ){
08
Var value = ['10', 'fillp ', 'abc'],
09
InsertSQLString = 'insert into teachers SET id = ?, Name =? , Pwd =? ',
10
SelectSQLString = 'select * from teachers ',
11
UpdateSQLString = "update teachers set NAME = 'ipone' where ID = 4 ",
12
DeleteSQLString = 'delete from teachers where ID = 10 ';
13
14
Console. log ('clientconnectionready '),
15
Console. log ('\ n ')
16
// Lzq is the name of database.
17
Client. query ('use lzq', function (error, results ){
18
If (error ){
19
Console. log ('clientconnectionready Error: '+ error. message ),
20
Client. end ();
21
Return;
22
}
23
Console. log ('ing ing to MySQL ...'),
24
Console. log ('ted CTED to MySQL automatically '),
25
Console. log ('\ n '),
26
Console. log ('Connection success ...'),
27
Console. log ('\ n '),
28
29
Client. end (),
30
Console. log ('Connection closed '),
31
Console. log ('\ n ');
32
});
33
};
CRUD. js ---------------------------------->
View sourceprint?
01
Var CRUD = {
02
_ Insert: function (client, insertSQLString, value)
03
{
04
Client. query (insertSQLString, value, function (error, results)
05
{
06
If (error)
07
{
08
Console. log ("ClientReady Error:" + error. message ),
09
Client. end ();
10
Return;
11
}
12
Console. log ('inserted: '+ results. affectedRows + 'row .'),
13
Console. log ('insert success ...');
14
}
15
);
16
},
17
_ Select: function (client, selectSQLString)
18
{
19
Client. query (selectSQLString, function selectCb (error, results, fields)
20
{
21
If (error)
22
{
23
Console. log ('getdata Error: '+ error. message ),
24
Client. end ();
25
Return;
26
}
27
If (results. length> 0)
28
{
29
Var firstResult,
30
ResultSet = '';
31
For (var I = 0, len = results. length; I <len; I ++)
32
{
33
FirstResult = results [I],
34
ResultSet + = 'id: '+ firstResult ['id'] + ''+ 'name:' + firstResult ['name'] +'' + 'pwd: '+ firstResult ['pwd'] +' \ n ';
35
}
36
}
37
Console. log (resultSet );
38
/* Add function: return the query result set to the client and ensure the universality of this function .*/
39
}
40
);
41
},
42
_ Update: function (client, updateSQLString)
43
{
44
Client. query (updateSQLString, function (error, results)
45
{
46
If (error)
47
{
48
Console. log ("ClientReady Error:" + error. message ),
49
Client. end ();
50
Return;
51
}
52
Console. log ('Update success ...');
53
}
54
);
55
},
56
_ Delete: function (client, deleteSQLString)
57
{
58
Client. query (deleteSQLString, function (error, results)
59
{
60
If (error)
61
{
62
Console. log ("ClientReady Error:" + error. message ),
63
Client. end ();
64
Return;
65
}
66
Console. log ('delete success ...');
67
}
68
);
69
}
70
};
71
Exports. _ insert = CRUD. _ insert,
72
Exports. _ select = CRUD. _ select,
73
Exports. _ update = CRUD. _ update,
74
Exports. _ delete = CRUD. _ delete;
From YouDoce