例子仅在windows下测试通过,没有放在linux下测试。如有问题,可以电邮给我~
1、安装node.js、mysql,此处略(自行搜索吧)…;
2、创建一个名为test的数据库,然后建一张名为user_info的表(仅供测试)…
这里假定mysql使用的用户名为root,密码为123456
相应的mysql如下:
复制代码 代码如下:
/**
* 创建名为test的数据库
*/
drop database if exists test;
create database test;
use test;
/**
* 创建user_info表
*/
drop table if exists `user_info`;create table `user_info` (
`userid` int(10) not null auto_increment,
`username` varchar(20) default null,
primary key (`userid`)
) engine=innodb auto_increment=4 default charset=utf8;
/**
* 插入三条记录
*/
insert into user_info values (null, '张一'), (null, '张二'), (null, '张三');
3、创建存储过程(写的很冗余,故意的… 正好学习一下语法>_
复制代码 代码如下:
delimiter $$
drop procedure if exists `test`.`proc_simple`$$
create procedure proc_simple(in uid int(10), out uname varchar(2), out totalcount int)
begindeclare str_name varchar(20);
set @str_name = '';
set totalcount = 0;
select count(1),username into totalcount,@str_name from user_info where userid = uid;
set uname = @str_name;
select uname, totalcount;
end$$
delimiter ;
4、写程序进行调用(假定存为名为sql.js的文件);
复制代码 代码如下:
/**
* created with jetbrains webstorm.
* user: meteoric_cry
* date: 12-12-28
* time: 上午00:18
* to change this template use file | settings | file templates.
*/
var mysql = require('mysql');var connection = mysql.createconnection({
host : 'localhost',
port : 3306,
user : 'root',
password : '123456',
database : 'test',
charset : 'utf8_general_ci',
debug : false
});
connection.connect();
connection.query('call proc_simple(1, @a, @b);', function(err, rows, fields) {
if (err) {
throw err;
}
var results = rows[0];
var row = results[0];
console.log(username:,row.uname, count:, row.totalcount);
});
connection.end();
5、运行示例程序;