Nodejs Operation MySQL

Source: Internet
Author: User

This article mainly introduces the Nodejs operation MySQL Database example, this article demonstrates how to create a MySQL connection in Nodejs, MySQL database, insert data, query data and other functions, the need for friends can refer to the following introduction: Following the previous Nodejs Hello, world! We can also see other strengths, Nodejs now the community's fiery, and a large number of engineers to its support, now has been led to a number of module out.

Content: The following is a demonstration of Nodejs's interaction with MySQL.

This is the time to join the MySQL module for Nodejs, which is what the previous chapter says about the NPM (Node Package Manager).

Load the MySQL module into the Nodejs:

Copy CodeThe code is as follows:
$NPM Install Mysql
JS Script Mysqltest.js
Copy CodeThe code is as follows:
Mysqltest.js
Load MySQL Module
var Client = require (' MySQL '). Client,
Client = new Client (),

The name of the database to create
Test_database = ' Nodejs_mysql_test ',
The name of the table to create
test_table = ' Test ';

User name
Client.user = ' root ';
Password
Client.password = ' root ';
Create a connection
Client.connect ();

Client.query (' CREATE DATABASE ' +test_database, function (err) {
if (err && err.number! = client.error_db_create_exists) {
throw err;
}
});

If No callback is provided, any errors'll be emitted as ' error '
Events by the client
Client.query (' use ' +test_database);
Client.query (
' CREATE TABLE ' +test_table+
' (ID INT (one) auto_increment, ' +
' Title VARCHAR (255), ' +
' Text text, ' +
' Created DATETIME, ' +
' PRIMARY KEY (id)) '
);

Client.query (
' INSERT into ' +test_table+ ' +
' SET title =?, Text =?, created =? ',
[' Super cool ', ' This was a nice text ', ' 2010-08-16 10:00:23 ']
);

var query = Client.query (
' INSERT into ' +test_table+ ' +
' SET title =?, Text =?, created =? ',
[' Another entry ', ' because 2 entries make a better test ', ' 2010-08-16 12:42:15 '
);

Client.query (
' SELECT * from ' +test_table,
function SELECTCB (Err, results, fields) {
if (err) {
throw err;
}

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


Execute script
Copy CodeThe code is as follows:
Node Mysqltest.js
The effect is as follows:

Nodejs Operation MySQL

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.