Preview:
connection to a database:
var mysql = require('mysql');
var con =mysql.createConnection({ host:"localhost",
user:"root", password:"123456", database:"emp"
});
con.connect(function(err) { if (err) throw err;
con.query("SELECT *FROM employ",function(err,result,fields){ if (err) throw err; console.log(result);
});
});
OUTPUT:
 

Step-2:
Creating a table:
var mysql = require('mysql');
var con =mysql.createConnection({ host:"localhost",
user:"root", password:"123456", database:"emp"
});
con.connect(function(err) { if (err) throw err; console.log("Connected!");
var sql="CREATE TABLE employ (name VARCHAR(255),address VARCHAR(255))"; con.query(sql,function(err,result) {
if (err) throw err; console.log("Table created");
});
});
Output:

Step-3:
Inserting into table:
var mysql = require('mysql');
var con =mysql.createConnection({ host:"localhost",
 
user:"root", password:"123456", database:"emp"
});
con.connect(function(err) { if (err) throw err; console.log("Connected!");
var sql="INSERT INTO employ (name,address)VALUES('PAVAN','SKP')";
con.query(sql,function(err,result){ if (err) throw err;
console.log("1 record inserted");
});
});
Output:

Step-4:
Displaying the data:
var mysql = require('mysql');
var con =mysql.createConnection({ host:"localhost",
user:"root", password:"123456", database:"emp"
});
con.connect(function(err) { if (err) throw err;
con.query("SELECT *FROM employ",function(err,result,fields){ if (err) throw err; console.log(result);
});
});
Output:

downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter