Node.js 连接 MySQL 并进行数据库操作

[1].[代码] 安装 node-mysql

1 $ npm install mysql

[2].[代码] 创建测试表

1 //数据库名 NodeSample
2  
3 CREATE TABLE`NodeSample`.`MyTable` (
4   `id` INT UNSIGNED NOTNULLAUTO_INCREMENT PRIMARYKEY,
5   `firstname`VARCHAR( 20 )NOTNULL ,
6   `lastname` VARCHAR( 20 ) NOTNULL,
7   `message` TEXTNOTNULL
8 ) ENGINE = MYISAM ;

[3].[代码] 连接数据库

01 var sys = require('sys');
02   
03 var Client = require('mysql').Client;
04 var client =newClient();
05   
06 client.user = 'someuser';
07 client.password = 'password';
08   
09 client.connect(function(error, results) {
10   if(error) {
11     console.log('Connection Error: '+ error.message);
12     return;
13   }
14   console.log('Connected to MySQL');
15 });

[4].[代码] 打开数据库

01 ClientConnectionReady = function(client)
02 {
03     client.query('USE NodeSample',function(error, results) {
04         if(error) {
05             console.log('ClientConnectionReady Error: '+ error.message);
06             client.end();
07             return;
08         }
09     });
10 };

[5].[代码] 完成数据库操作程序

01 var sys = require('sys');
02   
03 var Client = require('mysql').Client;
04 var client =newClient();
05   
06 client.user = 'someuser';
07 client.password = 'password';
08   
09 console.log('Connecting to MySQL...');
10   
11 client.connect(function(error, results) {
12   if(error) {
13     console.log('Connection Error: '+ error.message);
14     return;
15   }
16   console.log('Connected to MySQL');
17   ClientConnectionReady(client);
18 });
19   
20 ClientConnectionReady = function(client)
21 {
22     client.query('USE NodeSample',function(error, results) {
23         if(error) {
24             console.log('ClientConnectionReady Error: '+ error.message);
25             client.end();
26             return;
27         }
28         ClientReady(client);
29     });
30 };
31   
32 ClientReady = function(client)
33 {
34   varvalues = ['Chad','Lung','Hello World'];
35   client.query('INSERT INTO MyTable SET firstname = ?, lastname = ? , message = ?', values,
36     function(error, results) {
37       if(error) {
38         console.log("ClientReady Error: "+ error.message);
39         client.end();
40         return;
41       }
42       console.log('Inserted: '+ results.affectedRows +' row.');
43       console.log('Id inserted: '+ results.insertId);
44     }
45   );
46   GetData(client);
47 }
48   
49 GetData = function(client)
50 {
51   client.query(
52     'SELECT * FROM MyTable',
53     functionselectCb(error, results, fields) {
54       if(error) {
55           console.log('GetData Error: '+ error.message);
56           client.end();
57           return;
58       }
59       // Uncomment these if you want lots of feedback
60       //console.log('Results:');
61       //console.log(results);
62       //console.log('Field metadata:');
63       //console.log(fields);
64       //console.log(sys.inspect(results));
65   
66       if(results.length > 0)
67       {
68         varfirstResult = results[0];
69         console.log('First Name: '+ firstResult['firstname']);
70         console.log('Last Name: '+ firstResult['lastname']);
71         console.log('Message: '+ firstResult['message']);
72       }
73   });
74   
75   client.end();
76   console.log('Connection closed');
77 };

你可能感兴趣的:(NodeJs)