Tuesday, January 24, 2023

CRUD MySQL Pada Node.js

 


1. Insert Record

1.1. Insert Single

var mysql = require('mysql');  

var con = mysql.createConnection({  

host: "localhost",  

user: "root",  

password: "abcd",  

database: "dbMysql" 

});  

con.connect(function(err) {  

if (err) throw err;  

console.log("Connected!");  

var sql = "Insert Inyo employees (id, name, age, city) Values ('1', 'Ajeet Kumar', '27', 'Allahabad')";  

con.query(sql, function (err, result) {  

if (err) throw err;  

console.log("1 record inserted");  

});


1.2. Insert Multi Record

var mysql = require('mysql');  

var con = mysql.createConnection({  

host: "localhost",  

user: "root",  

password: "abcd",  

database: "dbMysql"  

});  

con.connect(function(err) {  

if (err) throw err;  

console.log("Connected!");  

var sql = "INSERT INTO employees (id, name, age, city) VALUES ?";  

var values = [  

['2', 'Bharat Kumar', '45', 'Mumbai'],  

['3', 'John  Tan', '25', ?Las Vegas'],  

['4', 'David Mur', '17', ?CA']  

];  

con.query(sql, [values], function (err, result) {  

if (err) throw err;  

console.log("Number of records inserted: " + result.affectedRows);  

});  

});


2. Delete Record

var mysql = require('mysql');  

var con = mysql.createConnection({  

host: "localhost",  

user: "root",  

password: "abcd",  

database: "dbMysql"  

});  

con.connect(function(err) {  

if (err) throw err;  

var sql = "DELETE FROM employees WHERE city = 'Malyasia'";  

con.query(sql, function (err, result) {  

if (err) throw err;  

console.log("Number of records deleted: " + result.affectedRows);  

});  

});


3. Select Record

3.1.  Select all data

var mysql = require('mysql');  

var con = mysql.createConnection({  

host: "localhost",  

user: "root",  

password: "abcd",  

database: "dbMysql"  

});  

con.connect(function(err) {  

if (err) throw err;  

con.query("SELECT * FROM employees", function (err, result) {  

if (err) throw err;  

console.log(result);  

});  

});


3.2  Select  Condition Unique

var mysql = require('mysql');  

var con = mysql.createConnection({  

host: "localhost",  

user: "root",  

password: "abcd",  

database: "dbMysql"  

});  

con.connect(function(err) {  

if (err) throw err;  

con.query("SELECT * FROM employees WHERE id = '1'", function (err, result) {  

if (err) throw err;  

console.log(result);  

});  

});


3.3. Select  Condition Wildcard

var mysql = require('mysql');  

var con = mysql.createConnection({  

host: "localhost",  

user: "root",  

password: "abcd",  

database: "dbMysql"  

});  

con.connect(function(err) {  

if (err) throw err;  

con.query("SELECT * FROM employees WHERE city LIKE 'M%'", function (err, result) {  

if (err) throw err;  

console.log(result);  

});  

});


4. Drop Table

var mysql = require('mysql');  

var con = mysql.createConnection({  

host: "localhost",  

user: "root",  

password: "abcd",  

database: "dbMysql"  

});  

con.connect(function(err) {  

if (err) throw err;  

var sql = "DROP TABLE employee2";  

con.query(sql, function (err, result) {  

if (err) throw err;  

console.log("Table deleted");  

});  

});


Memunculkan Simbol & Emoji Pada OS Mac

  Memunculkan Simbol & Emoji  1. Buka aplikasi Pages / Notes pada Macbook. 2. Klik pada Menubar Edit --> Pilih Emoji and Symbols a...