Node.js MySQL Insert Record

Websolutionstuff
1 min readDec 15, 2021

--

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!

--

--

Websolutionstuff
Websolutionstuff

Written by Websolutionstuff

I am Laravel and PHP Developer. I have also Good Knowledge of JavaScript, jQuery, Bootstrap and REST API. Visit Website: http://websolutionstuff.com/

No responses yet