AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Node.js MySQL Insert Into Table


<< Previous Page
Nodejs tutorial
Next Page >>
Nodejs tutorial

Node.js Tutorial » Node.js MySQL Insert Into Table

How to Insert Data into MySQL Database Using Node JS?

In this chapter you will learn how to insert a new record intoMySQL table from node.js application.

Use these steps to ionsert row into a table from nodejs:


1. First connect to MySQL db server.
2. Second Let’s, create a node_mysql_insert_tbl_test.js file and put the following code in it –
3. Close the DB connection.

 

Node.js MySql insert into Table

To fill a table in MySQL, use the "INSERT INTO" statement.

Let’s insert row into MySQL Table named “Users” under “my_db_name”.

Example: insert row into MySQ TABLE named "Users":

var mysql = require('mysql');

var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "Users;
});

conn.connect(function(err) {
if (err) throw err;
console.log("Connected!");

var sql = "INSERT INTO customers (name, address, email) VALUES ('Stwart A', 'Highways 27', 'stwarta@agernic.com')";
conn.query(sql, function (err, result) {
if (err) throw err;
console.log("row inserted");
});
});

Save the code above in a file named "demo_test_db_insert.js", and run the file:

E:\Users\Your_Name>node demo_test_db_insert.js

 

Related subjects:
node.js create database nodejs create table node.js Send Email upload files

 

node js insert into mysql, data into mongodb, into postgresql, json to mysql, into table, array, to sql server, query mysql, into, query in loop, insert variables, insert multiple rows, insert return id,
how to insert data from a form into database using node js
node js mysql insert variables
insert query in node js mssql
nodejs mysql insert multiple rows
nodejs mysql insert return id
how to insert data from a form into database using node js
node js mysql insert multiple rows json
how to return a value from a mysql select query in node js

 

Insert Multiple Records

To insert more record, you have to make an array containing the values, and insert a question mark in the sql

We create a demo_test_multi_rows_db_insert.js file and put the following code in it

Example: add, insert Multiple Records:

var mysql = require('mysql');

var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});

conn.connect(function(err) {
if (err) throw err;
console.log("Connected!");

var sql = "INSERT INTO customers (name, address, email) VALUES ?";
var values = [
['KeithN', '28 Amb', 'KeithN@agernic.com'],
['Alex Mar', '14 Cyber', 'Alex-Mar@agernic.com'],
['Murphy Jo', '15 Guion', 'Murphy-Jo@agernic.com'],
['Amanda Key', '35 Shefild', 'Amanda-Key@agernic.com'],
['Jason Rak', '31 Nako', 'Jason-Rak@agernic.com'],
];
conn.query(sql, [values], function (err, result) {
if (err) throw err;
console.log("Ok | MySQL Multiple Record Inserted");
console.log("No. of records inserted- " + result.affectedRows);
});
});

Save the code above in a file called "demo_test_multi_rows_db_insert.js", and run the file:

Example: : Run "demo_test_multi_rows_db_insert.js"
E:\Users\Your_Name>node demo_test_multi_rows_db_insert.js

 

Get Inserted ID

Insert a record in the "users" table, and return the ID.

Example:

var mysql = require('mysql');

var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});

conn.connect(function(err) {
if (err) throw err;
console.log("Connected!");

var sql = "INSERT INTO customers (name, address, email) VALUES ('Stwart A', 'Highways 27', 'stwarta@agernic.com')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted, ID: " + result.insertId);
});
});

Save the code above in a file called "demo_test_multi_rows_db_insert.js", and run the file:

Example: : Run "demo_test_multi_rows_db_insert.js"
E:\Users\Your_Name>node demo_test_multi_rows_db_insert.js

 



node js insert into mysql, data into mongodb, into postgresql, json to mysql, into table, array, to sql server, query mysql, into, query in loop, insert variables, insert multiple rows, insert return id,
how to insert data from a form into database using node js

Node.js MySQL Insert Into Table - nodejs

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Error: Unable to connect to MySQL. Debugging errno: 1045 Debugging error: Access denied for user 'u142985959_ager'@'localhost' (using password: YES)