nodejs mysql

mysql = require("mysql");

TEST_DATABASE = "vote";

TEST_TABLE = "test";

// Create the connection.

// Data is default to new mysql installation and should be changed according to your configuration.

var connection = mysql.createConnection({

   user: "root",

   password: "root",

   database: ""

});

connection.query('USE '+TEST_DATABASE);  

connection.query(  

  'CREATE TABLE '+TEST_TABLE+  

  '(id INT(11) AUTO_INCREMENT, '+  

  'title VARCHAR(255), '+  

  'text TEXT, '+  

  'created DATETIME, '+  

  'PRIMARY KEY (id));'  

);  

connection.query(  

  'INSERT INTO '+TEST_TABLE+' '+  

  'SET title = ?, text = ?, created = ?',  

  ['super cool', 'this is a nice text', '2010-08-16 10:00:23']  

);  

var query = connection.query(  

  'INSERT INTO '+TEST_TABLE+' '+  

  'SET title = ?, text = ?, created = ?',  

  ['another entry', 'because 2 entries make a better test', '2010-08-16 12:42:15']  

);  

console.log(query);

connection.query('SELECT * FROM ' +TEST_TABLE + ' limit 10;', function (error, rows, fields) {

console.log(JSON.stringify(rows));

});

connection.end();  

猜你喜欢

转载自michaelzqm.iteye.com/blog/1753936