Nodejs MySQL | Complete Setup Guide + CRUD 2021 | Part - B

nodejs mysql


Nodejs MySQL is getting popular as they mix very well together. Today we will get to know how to setup Nodejs mySQL, connect with database, create db and tables and perform CRUD operation with Nodejs and mySQL.

Introduction

Node.js is one of the most popular open-source, cross-platform backend runtime environment that uses JavaScript. It is mainly used for the purpose of using JavaScript code outside the web browser. Nowadays, Node JS has become popular with mostly NoSQL databases mostly MongoDB. As relational database system like MySQL is popular in the market today we have brought this complete guide on Nodejs MySQL.

You will learn to use Node.js with MySQL database. You will know how you can setup environment, database, tables and also perform CRUD (Create, Read, Update and Delete) operations with Nodejs MySQL.

CHECK OUT THE PART A OF THIS BLOG.

CRUD Operation with Nodejs MySQL

Create

To insert data into a table in MySQL, use the "INSERT INTO" statement.

const sql = "INSERT INTO users (name, email) VALUES ('John Watson', 'john@watson.com')";

con.query(sql, (err, result) => {
  if (err) throw err;
  console.log("1 record inserted");
});

Create Multiple Records

To insert more than one records, make an array containing the values, and insert a question mark in the sql, which will be replaced by the value array.

const sql = "INSERT INTO users (name, email) VALUES ?  ";
const values = [
  ["Ben", "ben@gmail.com"],
  ["Vicky", "vicky@gmail.com"],
];
con.query(sql, [values], (err, result) => {
  if (err) throw err;
  console.log(`${values.length()} records inserted`);
});

Read

To select data from a table in MySQL, use the "SELECT" statement.

//Select all the data from users table

const sql = "SELECT * FROM users";

con.query(sql, (err, result) => {
  if (err) throw err;
  console.log(result);
});

Read particular data

Get particular data from the table.

//Select particular data from the users table

const sql = "SELECT name FROM users";

con.query(sql, (err, result) => {
  if (err) throw err;
  console.log(result);
});

//Here we will get name of all users

Update

You can update existing records in a table by using the "UPDATE" statement.

const sql = "UPDATE users SET email = 'new@gmail.com' WHERE name = 'Vicky'";

con.query(sql, (err, result) => {
  if (err) throw err;
  console.log("Record Updated");
});

Delete

You can delete records from an existing table by using the "DELETE FROM" statement.

const sql = "DELETE FROM users WHERE name = 'Vicky' ";

con.query(sql, (err, result) => {
  if (err) throw err;
  console.log("Record Deleted");
});

Conclusion

In this blog, you have learnt how you can create data, insert data, read whole data, particular data, update and also delete the data. In this Part B of Nodejs MySQL you have learnt the basics of Nodejs with MySQL.

To get more tips, ticks and guides on Coding follow The Info Docx.
REFERENCE- Poor Coders


Also Read:

Tags: Tags: nodejs mysql, nodejs mysql2, nodejs and mysql, nodejs mysql query, nodejs mysql connection, nodejs mysql tutorial, nodejs mysql example, nodejs mysql insert, node js mysql query, nodejs mysql connection pool, nodejs with mysql, nodejs mysql library, nodejs mysql bulk insert, npm mysql, mysql2 npm, mysql js, node mysql2,npm mysql, express mysql, mysql2 npm, mysql js, npm install mysql, nodejs mysql connection, node js connect to mysql, express js mysql, mysql2 nodejs, mysql2 node, node js mysql query, mysql with node js, node js and mysql

Post a Comment

Previous Post Next Post