nodejs mysql

安装
npm install mysql

实例
http://www.neekey.net/wiki/doku.php?id=nodejs:nodejs%E4%B8%8Emysql

下面以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 

你可能感兴趣的:(mysql,function,table,database,insert,callback)