The example only passed the test under Windows, and was not tested under Linux. If you have any questions, you can email me ~
1. Install node.js and mysql, here is a little (search it yourself)...;
2. Create a database called test, and then create a table called user_info (for testing only)…
Here, it is assumed that the user name used by mysql is root and the password is 123456
The corresponding mysql is as follows:
The code copy is as follows:
/**
* Create a database named test
*/
DROP DATABASE IF EXISTS test;
CREATE DATABASE test;
USE test;
/**
* Create user_info table
*/
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 three records
*/
INSERT INTO user_info VALUES (NULL, 'Zhang Yi'), (NULL, 'Zhang Er'), (NULL, 'Zhang San');
3. Create stored procedures (written very redundantly, deliberately... Just learn grammar>_<);
The code copy is as follows:
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`proc_simple`$$
CREATE PROCEDURE proc_simple(IN uid INT(10), OUT uName VARCHAR(2), OUT totalCount INT)
BEGIN
DECLARE 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. Write the program to make calls (assuming that it is a file named sql.js);
The code copy is as follows:
/**
* Created with JetBrains WebStorm.
* User: Meteoric_cry
* Date: 12-12-28
* Time: 00:18 am
* 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. Run the sample program;