Chinaunix首页 | 论坛 | 博客
  • 博客访问: 652944
  • 博文数量: 26
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 3182
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-23 14:29
个人简介

7年游戏服务器开发,擅长c/c++,javesript,php;熟悉linux,mysql/redis,elasticsearch;开源爱好者.github : https://github.com/yuyunliuhen

文章分类

全部博文(26)

文章存档

2016年(1)

2015年(3)

2014年(3)

2013年(19)

分类: 数据库开发技术

2013-06-16 09:08:37

1 install node-mysql
  1. [lee@bogon node]$ npm install mysql
  2. npm http GET
  3. npm http 304
  4. npm http GET
  5. npm http GET
  6. npm http 304
  7. npm http 304
  8. mysql@2.0.0-alpha8 node_modules/mysql
  9. ├── require-all@0.0.3
  10. └── bignumber.js@1.0.1
2  create js file
[lee@bogon node]$ vi mysql_test.js
  1. var mysql = require('mysql');
  2. var TEST_DATABASE = 'db_test';
  3. var TEST_TABLE = 'table_test1';

  4. var connection = mysql.createConnection({
  5.     host : 'localhost',
  6.     user : 'root',
  7.     password : 'root',
  8. });

  9. connection.query('CREATE DATABASE '+TEST_DATABASE, function(err) {
  10.   if (err && err.number != mysql.ERROR_DB_CREATE_EXISTS) {
  11.     throw err;
  12.   }
  13. });

  14. connection.query('USE '+TEST_DATABASE);

  15. connection.query(
  16.   ' DROP TABLE '+'IF EXISTS '+TEST_TABLE
  17. );

  18. connection.query(
  19.   'CREATE TABLE '+TEST_TABLE+
  20.   '(id INT(11) AUTO_INCREMENT, '+
  21.   'name VARCHAR(255), '+
  22.   'PRIMARY KEY (id))'
  23. );

  24. connection.query(
  25.   'INSERT INTO '+TEST_TABLE+' '+
  26.   'SET name = ?',
  27.   ['lee']
  28. );

  29. var query = connection.query(
  30.   'INSERT INTO '+TEST_TABLE+' '+
  31.   'SET name = ?',
  32.   ['sophia']
  33. );

  34. connection.query(
  35.   'SELECT id,name FROM '+TEST_TABLE,
  36.   function selectCb(err, results, fields)
  37.   {
  38.     if (err) { throw err; }
  39.     for(var i = 0;i < results.length; ++i)
  40.     {
  41.         var data = 'id: ' + results[i]['id'] + ' name:' + results[i]['name'];
  42.         console.log(data);
  43.     }
  44.     console.log(results);
  45.     console.log(fields);
  46.     connection.end();
  47.   }
  48. );

3
run

[lee@bogon node]$ node mysql_test.js

4 output
  1. id: 1 name:lee
  2. id: 2 name:sophia
  3. [ { id: 1, name: 'lee' }, { id: 2, name: 'sophia' } ]
  4. [ { catalog: 'def',
  5. db: 'db_test',
  6. table: 'table_test1',
  7. orgTable: 'table_test1',
  8. name: 'id',
  9. orgName: 'id',
  10. filler1: ,
  11. charsetNr: 63,
  12. length: undefined,
  13. type: 3,
  14. flags: 16899,
  15. decimals: 0,
  16. filler2: ,
  17. default: undefined,
  18. zeroFill: false,
  19. protocol41: true,
  20. fieldLength: 11 },
  21. { catalog: 'def',
  22. db: 'db_test',
  23. table: 'table_test1',
  24. orgTable: 'table_test1',
  25. name: 'name',
  26. orgName: 'name',
  27. filler1: ,
  28. charsetNr: 33,
  29. length: undefined,
  30. type: 253,
  31. flags: 0,
  32. decimals: 0,
  33. filler2: ,
  34. default: undefined,
  35. zeroFill: false,
  36. protocol41: true,
  37. fieldLength: 765 } ]
阅读(4618) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~