Skip to content

Latest commit

 

History

History
76 lines (51 loc) · 1.67 KB

File metadata and controls

76 lines (51 loc) · 1.67 KB

20CYS202 - User Interface Design

20CYS202 - User Interface Design - 3rd Sem - B.E. CSE(CYS) - ASE, CBE

Database Connectivity using MySQL and Node.js

Prerequisites

  • MySQL 8.0
    • Install MySQL
    • Configure "username" and "password"
    • Create a database
  • Node.js
    • Install
  • NPM
    • Run "npm install mysql"

Example Codes

  • require includes the npm library "mysql"
  • con is the connection variable which creates a connection to mysql instance running in "localhost" with "root" as username and "password" as password to a database "demodb"
  • sqlquery variable should have the sql query to be executed

Create a connection

var mysql = require('mysql');

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

con.connect(function(err) {

  if (err) throw err;
  console.log("Connected!");
  
});

Execute a query

var mysql = require('mysql');

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

con.connect(function(err) {

  if (err) throw err;
  console.log("Connected!");
  
  var sqlquery = "CREATE TABLE CYS (name VARCHAR(255), rollno VARCHAR(255))";
  
  con.query(sqlquery, function (err, result) {
  
    if (err) throw err;
    console.log("Table created");
    
  });
  
});