Examples are tested only by Windows, not under Linux. If you have any questions, please email me
1, install Node.js, MySQL, here slightly (own search bar) ... ;
2, create a database named Test, and then build a table named User_info (test only) ...
This assumes that MySQL uses a username of root with a password of 123456
The corresponding MySQL is as follows:
Copy Code code 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 (a) not NULL auto_increment,
' userName ' varchar DEFAULT NULL,
PRIMARY KEY (' userId ')
) Engine=innodb auto_increment=4 DEFAULT Charset=utf8;
/**
* Insert three Records
*/
INSERT into User_info VALUES (null, ' one '), (null, ' Zhang Yi '), (null, ' John ');
3, the creation of stored procedures (write very redundant, intentional ...) Just learn grammar >_<);
Copy Code code as follows:
DELIMITER $$
DROP PROCEDURE IF EXISTS ' test '. ' Proc_simple ' $$
CREATE PROCEDURE proc_simple (in UID INT (Ten), 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 invoke (assumed to be stored as a file named Sql.js);
Copy code code as follows:
/**
* Created with JetBrains webstorm.
* user:meteoric_cry
* date:12-12-28
* time: Morning 00:18
* to change this temp Late Use File | Settings | File Templates.
*/
var mysql = require (' mysql ');
var connection = mysql.createconnection ({
host: ' localhost ',
p ort:3306,
User: ' Root ',
password: ' 123456 ',
datab ASE: ' Test ',
charset: ' Utf8_general_ci ',
debug:false
});
Connection.connect ();
Connection.query (' Call Proc_simple (1, @a, @b); ', function (err, rows, fields) {
if (err) {
&N bsp; throw err;
}
var results = rows[0];
var row = results[0];
&Nbsp; Console.log ("UserName:", Row.uname, "Count:", Row.totalcount);
});
Connection.end ();
5, run the sample program;