Node.js MySQL Insert Record

In this tutorial we will see how to insert data into MySQL table using Node.js. In previous node .js article I will give you example about how to connect MySQL database with Node.js and how to create MySQL database in Node.js.

So, now I will give you information about insert rows into mysql database table from node.js or insert multiple records into mysql using node.js. Before perform this article please make sure create database with connection. So, read below tutorial for connect database.

Read More : Node.js MySQL Create Database

Example :

var mysql = require('mysql');var con = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "websolutionstuff"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected..!!");
var sql = "INSERT INTO users (name, email) VALUES ('websolutionstuff', 'websolutionstuff@gmail.com')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Record Inserted Successfully!");
});
});

Run db_record_demo.js file

node db_record_demo.js

The response with console messages like

Connected..!!
Record Inserted Successfully!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Websolutionstuff

Websolutionstuff

96 Followers

I am Laravel and PHP Developer. I have also Good Knowledge of JavaScript, jQuery, Bootstrap and REST API.