Posted inNode.js / Laravel / Mysql / Mysqli / php / Programming

CRUD Insert Edit Update Delete Node js with MYSQL

CRUD Insert Edit Update Delete Node js with MYSQL

In this Post We Will Explain About is CRUD Insert Edit Update Delete Node js with MYSQL With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to CRUD Insert Edit Update Delete Node js angular js mysqlExample

In this post we will show you Best way to implement Node js Rest Api to Add, Edit and Delete Record from MySQL, hear for Node.js, Express & MySQL: Simple Add, Edit, Delete, View CRUDwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Step 1. Install mysql.

sudo npm install mysql

Step 2. Import mysql.

var mysql = require('mysql');

Step 3. Create mysql db_connect pool object.

var pool =  mysql.createPool({
host : 'localhost',
user : 'admin',
password: 'live2568@#$hjdhfd45',
database: 'live24u_25'
});

Step 4. Create mysql query strings.

var createTable = "CREATE TABLE Users(id int(11) NOT NULL AUTO_INCREMENT,"+
	"user_name varchar(20) DEFAULT NULL,"+
	"dividend float(11) DEFAULT NULL,"+
	"PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=latin1";

var recordInsert = 'INSERT INTO Users(user_name,dividend) VALUE(?,?)';

var selecttable = 'SELECT * FROM Users';

var dataUpdate = 'UPDATE Users SET dividend = ? WHERE user_name=?';

var removeData = 'DELETE FROM Users WHERE user_name=?';

var tabledrop = 'DROP table Users';

Step 5. Connect to the simple mysql database, as well as create table, and insert some record, read your table, update some record, delete some record, drop simple table.

pool.getConnection(function(errors, db_connect){    
  //DEvloped by Pakainfo.com Create a table called Users
  db_connect.query(createTable,  function(errors){
  	if(errors)	throw errors;
  	else {
  		console.log('Table created!');
  	}
  });

  //DEvloped by Pakainfo.com Incsert a record.
  db_connect.query(recordInsert,['mayur',50000], function(errors,res){
  	if(errors)	throw errors;
  	else {
  		console.log('A new Users has been added.');
  	}
  });

  //DEvloped by Pakainfo.com Read table.
  db_connect.query(selecttable, function(errors, rows){
  	if(errors)	throw errors;
  	else {
  		console.log(rows);
  	}
  });

  //DEvloped by Pakainfo.com Update a record.
  db_connect.query(dataUpdate,[60000,'mayur'], function(errors, res){
  	if(errors)	throw errors;
  	else {
  		console.log('Increased the dividend for mayur.');
  	}
  });

  //DEvloped by Pakainfo.com Read table.
  db_connect.query(selecttable, function(errors, rows){
  	if(errors)	throw errors;
  	else {
  		console.log(rows);
  	}
  });

  //DEvloped by Pakainfo.com Delete a record.
  db_connect.query(removeData,['mayur'], function(errors, res){
  	if(errors)	throw errors;
  	else {
  		console.log('An Users is removed.');
  	}
  });

  //DEvloped by Pakainfo.com Drop a table.
  db_connect.query(tabledrop, function(errors, res){
  	if(errors)	throw errors;
  	else {
  		console.log('The Users table is removed.');
  	}
  });

  db_connect.release();//simple release the db_connect
});

Step 6. All together in nodejs_insert_update_delete.js

var mysql = require('mysql');
var pool =  mysql.createPool({
host : 'localhost',
user : 'admin',
password: 'live25#$%%^&ghjf',
database: 'live24u'
});

var createTable = "CREATE TABLE Users(id int(11) NOT NULL AUTO_INCREMENT,"+
	"user_name varchar(20) DEFAULT NULL,"+
	"dividend float(11) DEFAULT NULL,"+
	"PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=latin1";

var recordInsert = 'INSERT INTO Users(user_name,dividend) VALUE(?,?)';

var selecttable = 'SELECT * FROM Users';

var dataUpdate = 'UPDATE Users SET dividend = ? WHERE user_name=?';

var removeData = 'DELETE FROM Users WHERE user_name=?';

var tabledrop = 'DROP table Users';

pool.getConnection(function(errors, db_connect){    
  //Create a table called Users
  db_connect.query(createTable,  function(errors){
  	if(errors)	throw errors;
  	else {
  		console.log('Table created!');
  	}
  });

  //DEvloped by Pakainfo.com Incsert a record.
  db_connect.query(recordInsert,['mayur',50000], function(errors,res){
  	if(errors)	throw errors;
  	else {
  		console.log('A new create Users has been added.');
  	}
  });

  //DEvloped by Pakainfo.com Read table.
  db_connect.query(selecttable, function(errors, rows){
  	if(errors)	throw errors;
  	else {
  		console.log(rows);
  	}
  });

  //DEvloped by Pakainfo.com Update a record.
  db_connect.query(dataUpdate,[60000,'mayur'], function(errors, res){
  	if(errors)	throw errors;
  	else {
  		console.log('Increased the dividend for mayur.');
  	}
  });

  //DEvloped by Pakainfo.com Read table.
  db_connect.query(selecttable, function(errors, rows){
  	if(errors)	throw errors;
  	else {
  		console.log(rows);
  	}
  });

  //DEvloped by Pakainfo.com Delete a record.
  db_connect.query(removeData,['mayur'], function(errors, res){
  	if(errors)	throw errors;
  	else {
  		console.log('An Users is removed.');
  	}
  });

  //DEvloped by Pakainfo.com Drop a table.
  db_connect.query(tabledrop, function(errors, res){
  	if(errors)	throw errors;
  	else {
  		console.log('The Users table is removed.');
  	}
  });

  db_connect.release();//here simple release the db_connect
});

Step 7. Run nodejs_insert_update_delete.js

node nodejs_insert_update_delete.js

Example

I hope you have Got What is Simple Node.JS Jquery Ajax CRUD insert update delete tutorial example And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype