返回顶部

收藏

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

更多

Node.js是一套用来编写高性能网络服务器的JavaScript工具包

安装 node-mysql

$ npm install mysql

创建测试表

//数据库名 NodeSample

CREATE TABLE `NodeSample`.`MyTable` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  `firstname` VARCHAR( 20 ) NOT NULL ,
  `lastname` VARCHAR( 20 ) NOT NULL ,
  `message` TEXT NOT NULL
) ENGINE = MYISAM ;

连接数据库

var sys = require('sys');

var Client = require('mysql').Client;
var client = new Client();

client.user = 'someuser';
client.password = 'password';

client.connect(function(error, results) {
  if(error) {
    console.log('Connection Error: ' + error.message);
    return;
  }
  console.log('Connected to MySQL');
});

打开数据库

ClientConnectionReady = function(client)
{
    client.query('USE NodeSample', function(error, results) {
        if(error) {
            console.log('ClientConnectionReady Error: ' + error.message);
            client.end();
            return;
        }
    });
};

完成数据库操作程序

var sys = require('sys');

var Client = require('mysql').Client;
var client = new Client();

client.user = 'someuser';
client.password = 'password';

console.log('Connecting to MySQL...');

client.connect(function(error, results) {
  if(error) {
    console.log('Connection Error: ' + error.message);
    return;
  }
  console.log('Connected to MySQL');
  ClientConnectionReady(client);
});

ClientConnectionReady = function(client)
{
    client.query('USE NodeSample', function(error, results) {
        if(error) {
            console.log('ClientConnectionReady Error: ' + error.message);
            client.end();
            return;
        }
        ClientReady(client);
    });
};

ClientReady = function(client)
{
  var values = ['Chad', 'Lung', 'Hello World'];
  client.query('INSERT INTO MyTable SET firstname = ?, lastname = ? , message = ?', values,
    function(error, results) {
      if(error) {
        console.log("ClientReady Error: " + error.message);
        client.end();
        return;
      }
      console.log('Inserted: ' + results.affectedRows + ' row.');
      console.log('Id inserted: ' + results.insertId);
    }
  );
  GetData(client);
}

GetData = function(client)
{
  client.query(
    'SELECT * FROM MyTable',
    function selectCb(error, results, fields) {
      if (error) {
          console.log('GetData Error: ' + error.message);
          client.end();
          return;
      }
      // Uncomment these if you want lots of feedback
      //console.log('Results:');
      //console.log(results);
      //console.log('Field metadata:');
      //console.log(fields);
      //console.log(sys.inspect(results));

      if(results.length > 0)
      {
        var firstResult = results[0];
        console.log('First Name: ' + firstResult['firstname']);
        console.log('Last Name: ' + firstResult['lastname']);
        console.log('Message: ' + firstResult['message']);
      }
  });

  client.end();
  console.log('Connection closed');
};

标签:Node.js,MySQL

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. polandeme 发表 2014-10-25 03:02:45 mysql数据库备份mysqldump操作
  2. David Walsh 发表 2015-04-29 21:51:08 Prevent Animated GIFs from Looping
  3. mdjhny 发表 2013-01-14 16:00:00 mysql+python使用心得
  4. 博主 发表 2014-01-21 05:04:00 安装NewRelic的MySQL插件
  5. 博主 发表 2015-04-12 13:51:00 玩转Node.js(四)-搭建简单的聊天室
  6. 有梦最美,希望相随 发表 2015-04-08 07:18:44 Rails 4, MySQL, and Emoji (Mysql2::Error: Incorrec
  7. saymoon 发表 2011-03-16 10:53:59 MAC OS 下自带php.ini配置
  8. sulong 发表 2007-11-20 05:36:56 mysql sql 语句中的大小写规则
  9. 博主 发表 2015-01-15 06:46:48 MySQL 知识点整理(中)
  10. Sun 发表 2013-04-01 08:35:32 PHP MySQL pconnect()
  11. francs 发表 2014-12-05 06:36:55 MySQL:分区表之二:LIST 分区
  12. 博主 发表 2014-08-30 10:00:37 MySQL建立唯一索引去重失效

发表评论