Nodejs Course

- Create Database

Node.js can be used in applications with MySQL database.
If you haven't MySQL database installed on your operating system, you can download a free MySQL database at:
//dev.mysql.com/downloads/mysql/

Once you have MySQL up and running on your computer, you can access it with Node.js.
To access a MySQL database with Node.js, you need a MySQL driver. This tutorial will use the "mysql" module, from NPM.
To install the "mysql" module, open the Command Line interface and execute the following:
npm install --save mysql
Now, you can connect to MySQL, and perform SQL queries in your Node.js applications.

Connect to MySQL

To connect to MySQL server, include "mysql" module (with require()), and use the mysql.createConnection() method, with the "username" and "password" from your MySQL database; then the connect() method to establish the connection.
- Here is an example of connecting to MySQL server:
const mysql = require('mysql');

const con = mysql.createConnection({
  host: 'localhost',
  user: 'name',
  password: 'pass'
});

con.connect(err=>{
  if(err) throw err;
  console.log('Connected to mysql');
});
Save the code above in a file called "mysql_con.js" (for example, in a "test/" folder in "nodejs") and run the file in command line interface:
node test/mysql_con.js
Which will give you this result:
Connected to mysql
- To terminate a connection, you can use the con.end() or con.destroy() method. The end() method takes a callback argument.
con.end(err=>{
  if(err) throw err;
  console.log('Connection is terminated');
});

Create Database

The connection object created in the example above ("con"), has a query() method for querying the database.
The query() method establishes the connection; it takes a sql statements as a parameter, and returns the result in a callback function.
Will use this method to perform SQL queries, to create the database, and to read from (or write to) a MySQL database.

To store data in MySQL, you need to create a database. You can create a database with the "CREATE DATABASE" statement.
- Lets create a database called "nodedb"
const mysql = require('mysql');

const con = mysql.createConnection({
  host: 'localhost',
  user: 'name',
  password: 'pass'
});

let sql ='CREATE DATABASE nodedb';

con.query(sql, (err, res)=>{
  if(err) throw err;
  console.log('Database created.', res);

  con.end(err=>{
    if(err) throw err;
    console.log('Connection is terminated');
  });
});
Save the code above in a file called "mysql_create_db.js" and run the file:
node test/mysql_create_db.js

If you notice that the default collation of MySql is set to "latin1_swedish_ci", to change the default collation to "utf8_general_ci" in MySql:
- Open the "my.ini" file (mysql/bin/my.ini), find the text [mysqld] and add these lines:

character-set-server = utf8
collation-server = utf8_general_ci

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
}
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
alert(nr_divs);
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
Node.js with MySQL Database

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (137890)
  2. The Stage, Panels and Tools in Flash (10203)
  3. PHP Unzipper - Extract Zip, Rar Archives (31816)
  4. Arrays in ActionScript 3 (3091)
  5. String Object (667)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (203)
  2. Read Excel file data in PHP - PhpExcelReader (66)
  3. The Mastery of Love (56)
  4. PHP Unzipper - Extract Zip, Rar Archives (52)
  5. Working with MySQL Database (34)
Chat
Chat or leave a message for the other users
Full screenInchide