Node.js MySQL Insert Into

Node.js MySQL Insert Into

MySQL package is used to insert data into table in mysql using node js.

INSERT INTO mysql statement is used to insert data into table in mysql database.

Example : 

const mysql = require('mysql');

 

const db = mysql.createConnection({

     host: "localhost",
     user: "root",
     password: "*******",
     database : "probdb"


  });

// connect to database

db.connect((err) => {

    if (err) {

        throw err;

    }else{

     console.log('Connected to database!');

     db.query("INSERT INTO employee (emp_name, emp_address) VALUES ('manoj', 'aryatechno,india')", function (err, result) {
          if (err) throw err;
              console.log("New record is inserted!");
          }); 
    }

});

Write above code in server.js file and run this program as below.

D:\nodejs\website> node server.js  

It gives output as below.

Connected to database! New record is inserted!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

56023