Add mysql plug-in to nodejs

Source: Internet
Author: User

Install mysql plug-in
Npm install mysql@2.0.0-alpha7

Location after successful installation:
C: \ Users \ User Name \ node_modules \ mysql

How to view the readme. md File
Http://daringfireball.net/projects/markdown/dingus

Small mysql example

Var mysql = require ('mysql ');

Var TEST_DATABASE = 'nodejs _ db ';
Var TEST_TABLE = 'test ';

// Create a connection
Var client = mysql. createClient ({
User: 'root ',
Password: 'rainbow ',
});

// Create a database
Client. query ('create database' + TEST_DATABASE, function (err ){
If (err & err. number! = Mysql. ERROR_DB_CREATE_EXISTS ){
Throw err;
}
});

// The callback function is not specified. If an error occurs, the callback function is displayed as a client error.
Client. query ('use' + TEST_DATABASE );

// Create a table and insert data
Client. query (
'Create table' + TEST_TABLE +
'(Id INT (11) AUTO_INCREMENT,' +
'Name VARCHAR (255), '+
'Primary KEY (id ))'
);

Client. query (
'Insert INTO '+ TEST_TABLE + ''+
'Set name =? ',
['Nodejs1 ']
);

Var query = client. query (
'Insert INTO '+ TEST_TABLE + ''+
'Set name =? ',
['Nodejs2 ']
);

// Query and set the callback function
Client. query (
'Select * from' + TEST_TABLE,
Function selectCb (err, results, fields ){
If (err ){
Throw err;
}

Console. log (results );
Console. log (fields );
Client. end ();
}
);

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.