欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

NodeJS與Mysql的交互示例代碼

 更新時(shí)間:2013年08月18日 10:22:17   作者:  
實(shí)現(xiàn)NodeJS與Mysql的交互首先把Mysql Module裝到NodeJS中,具體實(shí)現(xiàn)及結(jié)果截圖如下,有此需求的朋友可以參考下,希望對(duì)大家有所幫助
把Mysql Module裝到NodeJS中

Js代碼
復(fù)制代碼 代碼如下:

$npm install Mysql

JS腳本 mysqlTest.js
Js代碼
復(fù)制代碼 代碼如下:

// mysqlTest.js
//加載mysql Module
var Client = require('mysql').Client,
client = new Client(),
  
  //要?jiǎng)?chuàng)建的數(shù)據(jù)庫名
TEST_DATABASE = 'nodejs_mysql_test',
//要?jiǎng)?chuàng)建的表名
TEST_TABLE = 'test';

//用戶名
client.user = 'root';
//密碼
client.password = 'root';
//創(chuàng)建連接
client.connect();

client.query('CREATE DATABASE '+TEST_DATABASE, function(err) {
if (err && err.number != Client.ERROR_DB_CREATE_EXISTS) {
throw err;
}
});

// If no callback is provided, any errors will be emitted as `'error'`
// events by the client
client.query('USE '+TEST_DATABASE);
client.query(
'CREATE TABLE '+TEST_TABLE+
'(id INT(11) AUTO_INCREMENT, '+
'title VARCHAR(255), '+
'text TEXT, '+
'created DATETIME, '+
'PRIMARY KEY (id))'
);

client.query(
'INSERT INTO '+TEST_TABLE+' '+
'SET title = ?, text = ?, created = ?',
['super cool', 'this is a nice text', '2010-08-16 10:00:23']
);

var query = client.query(
'INSERT INTO '+TEST_TABLE+' '+
'SET title = ?, text = ?, created = ?',
['another entry', 'because 2 entries make a better test', '2010-08-16 12:42:15']
);

client.query(
'SELECT * FROM '+TEST_TABLE,
function selectCb(err, results, fields) {
if (err) {
throw err;
}

console.log(results);
console.log(fields);
client.end();
}
);

執(zhí)行腳本
Js代碼
復(fù)制代碼 代碼如下:

root@sammor-desktop:/var/iapps/nodejs/work# node mysqlTest.js

相關(guān)文章

最新評(píng)論