Node.js MySQL Insert Record
In this post, we will discuss how to insert rows/records in MySQL database in a XAMPP Server using Node.js Script.
It is important to install mysql package in node.js
Command to install the mysql package:
npm install mysql
Steps:
Now let's see steps
- First start your XAMPP Server (Both Apache and MySQL).
- Open Notepad or any text-editor and write the Node.js script
- In that script, first we have to load the mysql package using the below syntax var mysql_package = require('mysql');
- Create the connection using the server,username and password.
- Write the sql query to insert a record in a table
- Now type the following command in your command prompt to run the script. node file_name.js
var connection_data = mysql_package.createConnection({
host: "localhost",
user: "root",
password: "",
database:"database_name"
});
Query method will take two parameters. The first parameter is the SQL Query and the second parameter will handle the result.
If we want to insert more than one row at a time, then you need to pass three parameters. second parameter is list of rows variable
For Single Row:
connection_data.connect(function(err) {
var sql = "INSERT INTO table_name VALUES (value1,value2...........)";
connection_data.query(sql, function (err, result) {
console.log("Row inserted Successfully!");
});
});
For Multiple Rows:
connection_data.connect(function(err) {
var sql = "INSERT INTO table_name VALUES ?";
var rows = [
[value1,....],.....
[value1,....],
];
connection_data.query(sql, [rows], function (err, result) {
console.log("Successfully Inserted");
});
});
INSERT record Example 1:-
Let's insert a record in a village table.
// Load the mysql package
var mysql_package = require('mysql');
// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
host: "localhost",
user: "root",
password: "",
database:"facility"
});
connection_data.connect(function(err) {
// Insert one record in village table.
var sql = "INSERT INTO village VALUES ('Kakumanu', 'Guntur',110)";
connection_data.query(sql, function (err, result) {
console.log("Row inserted Successfully!");
});
});
Output:
Row inserted Successfully!
So the table is:
It can also be possible to insert multiple records at a time.
INSERT record Example 2:-
Let's insert 4 records in a village table.
// Load the mysql package
var mysql_package = require('mysql');
// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
host: "localhost",
user: "root",
password: "",
database:"facility"
});
connection_data.connect(function(err) {
//Insert 4 records into village table.
var sql = "INSERT INTO village VALUES ?";
var rows = [
['delhi', 'delhi',30],
['patna', 'patna',100],
['bapatla', 'guntur',40],
['gogulamudi', 'guntur',67]
];
connection_data.query(sql, [rows], function (err, result) {
console.log("Successfully Inserted");
});
});
Output:
Successfully Inserted
So the table is:
SummarySo we seen how to insert record/s in XAMPP Server through Node.js script with two examples. make sure that you have to install mysql package and xampp server. It is important to pass the multiple rows list variable in query as second parameter.