Example of calling MySQL stored procedures in Node.js _node.js

Source: Internet
Author: User

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;

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.