初学nodejs 之 nodejs操作mysql

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);

3:运行效果:


4:查看mysql结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值