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

Source: Internet
Author: User
This article mainly introduces Node. example of calling the mysql stored procedure in js. this article passes the test in the windows environment. This article also provides examples of creating a database, inputting data, creating a stored procedure, and calling a stored procedure, for more information, see the example below. it is only tested in windows and 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:

The 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 >_< );

The 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 );

The 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;

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.