Example of calling the mysql stored procedure in Node. js, node. jsmysql
The test passed only in windows, but not in linux. If you have any questions, please email me ~
1. Install node. js and mysql. Click here (search by yourself )...;
2. Create a database named test and create a table named user_info (for test only )...
Assume that mysql uses the root user name and password 123456.
The corresponding mysql is as follows:
Copy codeThe Code is as follows:
/**
* Create a database named test
*/
Drop database if exists test;
Create database test;
USE test;
/**
* Create a 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 2'), (NULL, 'zhang san ');
3. Create a stored procedure (writing is redundant, deliberately... Just learn the syntax >_< );
Copy codeThe Code 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 a program for calling (assuming that the file named SQL. js is saved );
Copy codeThe Code is as follows:
/**
* Created with JetBrains WebStorm.
* User: Meteoric_cry
* Date: 12-12-28
* Time: 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: '000000 ',
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;