You are on page 1of 5

Lab Practical Submission

Date: 19/03/24
Roll No. and Name: 22BCE305 ROSHNI PANKAJKUMAR RANA
Course Code and Name: 2CS201 FULL STACK WEB DEVELOPMENT

Practical-8:

Demonstrate Node.js using MySQL to perform the below operations: Create a


database, create a table, insert a record, and update the record.

1. Create database

p8-1.js

var mysql = require('mysql2');

var con = mysql.createConnection({

host: "localhost",

user: "root",

password: "Roshni123"

});

con.connect(function(err) {

if (err) throw err;

console.log("Connected!");

con.query("CREATE DATABASE database1", function


(err, result) {

if (err) throw err;


console.log("Database created");

});

});

Output

2. Create a table

p8-2.js
var mysql = require('mysql2');

var con = mysql.createConnection({


host: "localhost",
user: "root",
password: "Roshni123",
database: "database1"
});

con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "CREATE TABLE employees (EID INT, Name
VARCHAR(25), Department VARCHAR(25))";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
});

Output

3. Insert a record

p8-3.js
var mysql = require('mysql2');

var con = mysql.createConnection({


host: "localhost",
user: "root",
password: "Roshni123"
});

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

4. Update a record
p8-4.js
var mysql = require('mysql2');

var con = mysql.createConnection({


host: "localhost",
user: "root",
password: "Roshni123",
database: "database1"
});

//show all records


con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM employees", function (err,
result, fields) {
if (err) throw err;
console.log("Records before updating");
console.log(result);
});
});

//updating
con.connect(function(err) {
if (err) throw err;
var sql = "UPDATE employees SET Department =
'Sales' WHERE Name = 'Mitalee'";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result.affectedRows + " record(s)
updated");
});
});

con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM employees", function
(err, result, fields) {
if (err) throw err;
console.log("Records after updating");
console.log(result);
});
});

Output

You might also like