Node.js MySQL Insert Records

📔 : NodeJS 🔗

पिछले topic में आपने सीखा कि Node.js में MySQL Tables कैसे create / delete करते हैं , इस topic में आप सीखेंगे कि किसी Table में कैसे records insert या exist record को update करते हैं।

Node.js MySQL Insert Record

तो Table में record insert करने के लिए हमें define किये गए attributes / columns and उनके type के according values insert करनी होती हैं। और जिन fields में value insert करनी होती है , सिर्फ उन्ही fields को select किया जाता है।


अगर आपको याद हो, पिछले Topic में एक tbl_users name की table बनायी थी जिसमे 6 attributes / columns define किये गए थे जिनमे से 2 attributes (id , create_date) की value default set की थी। तो insert करते समय इनकी values हमें insert नहीं करनी पड़ेगी।

Node.js MySQL Insert Record Example

Copy Fullscreen Close Fullscreen
// import mysql package
const mysql_module = require('mysql');
const mysql = mysql_module.createConnection({
  host: "localhost",
  user: "root",
  password: null,
  database : "node_mysql"
});

// make connection
mysql.connect(function(err) {
  if (err) throw err
  
  //insert query.
  let query = `INSERT INTO tbl_users(first_name, last_name, email, about_user) VALUES 
  ('Rahul', 'Kumar', 'info@learnhindituts.com', 'I am a student')`; 

  mysql.query(query, (error, result) => {
    if (error) throw error
    console.log("Record Inserted : ", result);
  })
});
C:\Users\HP\Desktop\workspace\nodejs>node app.js
Record Inserted :  OkPacket {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 1,
  serverStatus: 2,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0
}

Explain :
दिए गए example में सिर्फ 4 fields की value ही insert की गयी है। चूंकि इन fields का type VARCHAR था इसलिए इनकी value String की तरह insert की गयी है। query output में OkPacket Object में हमें affectedRows : 1 और insertId: 1 (Insert किये गए record की ID) भी मिली है , इसका मतलब है table में data insert हो गया है। अगर आप Table में देखेंगे तो पाएंगे कि record insert हो गया है।

Node.js MySQL Insert Multiple Records

multiple records insert करने के लिए simply MySQL query में columns को select करके values के बाद question mark (?) append करके query को run कर दिया जाता है। और values को एक Two Dimensional array में रखकर mysql.query() function में pass कर दिया जाता है।

Copy Fullscreen Close Fullscreen
// import mysql package
const mysql_module = require('mysql');
const mysql = mysql_module.createConnection({
  host: "localhost",
  user: "root",
  password: null,
  database : "node_mysql"
});

// make connection
mysql.connect(function(err) {
  if (err) throw err
  
  //insert multiple records.
  let query = `INSERT INTO tbl_users(first_name, last_name, email, about_user) VALUES ?`
  let values_array = [
    ['Rohan', 'Sharma', 'rohan@gmail.com', 'About rohan'],
    ['Shohan', 'verma', 'Shohan@gmail.com', 'About Shohan'],
    ['Mohan', 'yadav', 'Mohan@gmail.com', 'About Mohan'],
    ['Ravi', 'singh', 'Ravi@gmail.com', 'About Ravi'],
  ]

  //pass two dimensional array in a, array.
  mysql.query(query, [values_array], (error, result) => {
    if (error) throw error
    console.log("Records Inserted : ", result);
  })
});
C:\Users\HP\Desktop\workspace\nodejs>node app.js
Records Inserted :  OkPacket {
  fieldCount: 0,
  affectedRows: 4,
  insertId: 2,
  serverStatus: 2,
  warningCount: 0,
  message: '&Records: 4  Duplicates: 0  Warnings: 0',
  protocol41: true,
  changedRows: 0
}

query output में , आप साफ़ देख सकते हैं कि 4 new rows add हो गयी है , तो कुछ इस तरह से multiple records को insert किया जाता है।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers