/**
* Created by jiawei.tu on 8/7 0007.
*/
var mysql = require('mysql');
var async = require('async');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'fighting',
database: 'world'
});
connection.connect(function(err) {
if (err) {
console.log(err);
return;
}
console.log('connected as id ' + connection.threadId);
async.series([
function (callback) {
createPersonTableFunc(callback);
},
function (callback) {
insertPersonTableFunc(callback);
},
function (callback) {
queryCityFunc(callback);
}
], function (err, result) {
console.log(err ? err : result);
});
});
function createPersonTableFunc(callback) {
var createPersonTable = 'CREATE TABLE IF NOT EXISTS `person` (number INT(11), name VARCHAR(255), birthday BIGINT)';
connection.query(createPersonTable, function(err) {
console.log("11111");
callback(null, 1);
});
}
function insertPersonTableFunc(callback) {
var insertPersonTable = 'INSERT INTO person SET number=?, name=?, birthday=?';
connection.query(insertPersonTable, [123, 'hello', Date.now()], function(err, result) {
console.log("2222222");
callback(null, 2);
});
}
function queryCityFunc(callback) {
var selectCity = 'SELECT * FROM person';
connection.query(selectCity, function (err, result, fields) {
result.map(function (item) {
console.log(item);
});
console.log("333333");
callback(null, 3);
});
}
node.js mysql
最新推荐文章于 2024-03-11 09:16:06 发布