1:npm获取mysql包
打开cmd,cd到nodejs的安装目录,运行:npm install mysql。
2:nodejs代码:
var http = require('http'),
util = require('util'),
mysql = require('mysql');
http.createServer(function(req,res){
res.writeHead(200,{'Content-type':'text/html'});
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'sanyue'
});
var sql = [ 'set names utf8;',
'create database if not exists nodejs;',
'use nodejs;',
'drop table if exists nodejs;',
'create table nodejs ( id int(4), name char(12), age int(2));',
'insert into nodejs values (1,"John",20),(2,"Tom",21);',
'select * from nodejs;'
];
connection.query(sql[0],function(err,result,fields){
if(err){
res.write('<span style="color:orange;">Execute SQL:</span><span style="color:red;"> "'+sql[0]+'" failed!</span><br/>');
res.write(util.inspect(err,true));
res.end();
}else{
res.write('<span style="color:orange;">Execute SQL:</span><span style="color:green;"> "'+sql[0]+'" successed!</span><br/>');
}
});
connection.query(sql[1],function(err,result,fields){
if(err){
res.write('<span style="color:orange;">Execute SQL:</span><span style="color:red;"> "'+sql[1]+'" failed!</span><br/>');
res.write(util.inspect(err,true));
res.end();
}else{
res.write('<span style="color:orange;">Execute SQL:</span><span style="color:green;"> "'+sql[1]+'" successed!</span><br/>');
}
});
connection.query(sql[2],function(err,result,fields){
if(err){
res.write('<span style="color:orange;">Execute SQL:</span><span style="color:red;"> "'+sql[2]+'" failed!</span><br/>');
res.write(util.inspect(err,true));
res.end();
}else{
res.write('<span style="color:orange;">Execute SQL:</span><span style="color:green;"> "'+sql[2]+'" successed!</span><br/>');
}
});
connection.query(sql[3],function(err,result,fields){
if(err){
res.write('<span style="color:orange;">Execute SQL:</span><span style="color:red;"> "'+sql[3]+'" failed!</span><br/>');
res.end();
}else{
res.write('<span style="color:orange;">Execute SQL:</span><span style="color:green;"> "'+sql[3]+'" successed!</span><br/>');
}
});
connection.query(sql[4],function(err,result,fields){
if(err){
res.write('<span style="color:orange;">Execute SQL:</span><span style="color:red;"> "'+sql[4]+'" failed!</span><br/>');
res.write(util.inspect(err,true));
res.end();
}else{
res.write('<span style="color:orange;">Execute SQL:</span><span style="color:green;"> "'+sql[4]+'" successed!</span><br/>');
}
});
connection.query(sql[5],function(err,result,fields){
if(err){
res.write('<span style="color:orange;">Execute SQL:</span><span style="color:red;"> "'+sql[5]+'" failed!</span><br/>');
res.write(util.inspect(err,true));
res.end();
}else{
res.write('<span style="color:orange;">Execute SQL:</span><span style="color:green;"> "'+sql[5]+'" successed!</span><br/>');
}
});
connection.query(sql[6],function(err,result,fields){
if(err){
res.write('<span style="color:orange;">Execute SQL:</span><span style="color:red;"> "'+sql[6]+'" failed!</span><br/>');
res.write(util.inspect(err,true));
res.end();
}else{
res.write('<span style="color:orange;">Execute SQL:</span><span style="color:green;"> "'+sql[6]+'" successed!</span><br/>');
res.write('<span style="color:orange;">Result:</span><br/><span style="color:green;margin-left:30px;">'+util.inspect(result,true)+'</span><br/>');
res.end();
}
});
}).listen(9999);