日期:2014-05-16 浏览次数:20699 次
var mysql = require('mysql');
console.log("over-------------------------");
var TEST_DATABASE = 'nodejs_mysql_test';
var TEST_TABLE = 'test';
var client = mysql.createClient({
user: 'root',
password: 'root',
});
client.query('CREATE DATABASE '+TEST_DATABASE, function(err) {
if (err && err.number != mysql.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 TEMPORARY 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();
}
);$ node /example/mysql/mysqltest.js
Administrator@WIN-23C1Q4GKQ4G ~
$ node /example/mysql/mysqltest.js
over-------------------------
/example/mysql/mysqltest.js:12
throw err;
^
Error: ENOTFOUND, Domain name not found
at IOWatcher.callback (dns.js:74:15)
function Client() {
if (!(this instanceof Client) || arguments.length) {
throw new Error('deprecated: use mysql.createClient() instead');
}
EventEmitter.call(this);
this.host = 'localhost';
this.port = 3306;
this.user = 'root';
this.password = null;
this.database = '';
this.typeCast = true;
this.flags = Client.defaultFlags;
this.maxPacketSize = 0x01000000;
this.charsetNumber = constants.UTF8_UNICODE_CI;
this.debug = false;
this.ending = false;
this.connected = false;
this._greeting = null;
this._queue = [];
this._socket = null;
this._parser = null;
};
client.host = '127.0.0.1'; client.port = 3306;