Example of calling the mysql stored procedure in Node. js, node. jsmysql

Source: Internet
Author: User
Tags install node

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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.