Monday, 11 December 2017

Node.js | Creating a Database

To create a database in MySQL, use the "CREATE DATABASE" statement.

Example
This code creates a database named "nodedb".

creat_db.js
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "root"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  con.query("CREATE DATABASE nodedb", function (err, result) {
    if (err) throw err;
    console.log("Database created");
  });
});

Output
C:\Users\Your Name>node nodedb.js
Connected!
Database created

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...