nodejs mysql

安装
npm install mysql

实例

下面以ubuntu下为例

  1. 安装mysql module 
     $ npm install mysql
  2. 新建一个文件 mysql.js
  3. 输入如下内容 
    var Client = require('mysql').Client,  
        client = new Client();
     
    client.password = 'helloworld';
    client.user = 'root';
     
    var DATABASE_NAME = 'neekey_database',
    TABLE_NAME = 'neekey_table';
     
    console.log(client.connect());
     
    client.query('CREATE DATABASE '+ DATABASE_NAME, function(err) { 
      if (err && err.number != Client.ERROR_DB_CREATE_EXISTS) {  
     
      console.log(err);
        throw err;  
      }  
    });  
    // If no callback is provided, any errors will be emitted as `'error'`  
    // events by the client  
    client.query('USE '+ DATABASE_NAME);  
    client.query(  
      'CREATE TABLE '+ TABLE_NAME +  
      '(id INT(11) AUTO_INCREMENT, '+  
      'title VARCHAR(255), '+  
      'text TEXT, '+  
      'created DATETIME, '+  
      'PRIMARY KEY (id))'  
    );  
     
    client.query(  
      'INSERT INTO '+ TABLE_NAME +' '+  
      'SET title = ?, text = ?, created = ?',  
      ['super cool', 'this is a nice text', '2010-08-16 10:00:23']  
    );  
     
    var query = client.query(  
      'INSERT INTO '+ TABLE_NAME +' '+  
      'SET title = ?, text = ?, created = ?',  
      ['another entry', 'because 2 entries make a better test', '2010-08-16 12:42:15']  
    );  
     
    client.query(  
      'SELECT * FROM '+ TABLE_NAME,  
      function selectCb(err, results, fields) {  
        if (err) {  
          throw err;  
        }  
     
        console.log(results);  
        console.log(fields);  
        client.end();  
      }  
    );  
  4. 然后运行该文件 
     node mysql.js 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值