Example of operating a MySQL database in Nodejs _node.js

Source: Internet
Author: User
Tags create database

Foreword: Following the Nodejs of the previous hello,world! We can also see other powerful, Nodejs now the community's fiery, and a large number of engineers to support it, now has led to a large number of module out.

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

There is a need to join the MySQL module for Nodejs, and the NPM (Node Package Manager) mentioned in the previous chapter is starting to work.

Install MySQL module into Nodejs:

Copy Code code as follows:

$NPM Install Mysql



js script Mysqltest.js


Copy Code code as follows:

//mysqltest.js
//loading MySQL Module
var Client = require (' MySQL '). Client,
Client = new Client (),

//database name to be created
Test_database = ' nodejs_mysql_test ',
//table name to create
TE st_table = ' test ';

//username
Client.user = ' root ';
//password
Client.password = ' root ';
//Create 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 would be emitted as "error"
//Events by the client
Client.query (' US E ' +test_database);
Client.query (
' CREATE TABLE ' +test_table+
' (ID INT (one) auto_increment, ' +
' title VARCHAR (255), ' +
' te XT TEXT, ' +
' created DATETIME, ' +
' PRIMARY KEY (ID) '
);

Client.query (
INSERT into ' +test_table+ ' +
' SET title =?, Text =?, created =? ')
[' Super cool', ' is 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 E Rr
}

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




Execute Script


Copy Code code as follows:

Node Mysqltest.js



effect is as follows:

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.