0

I have written the following code to insert set of records by reading JSON using for loop.

This is my code:

node.js:

var MySQLHelper = {
composeQuery:
    function(stpName, paramArray) {
        var statement = "call " +  stpName + "(";
        for(var i = 0, len = paramArray.length; i < len; ++i) {
            var p = paramArray[i];
            statement +=  "'" + p +"'"; // add a parameter
            if (i != len-1 ) {
                statement +=  ",";      // add a parameter seperater
            }
            else {
                statement += ")";       // closing statement
            }
        }
        return statement;
    }
}

Dam.prototype.InsertPreset = function(taskInfo) {
// Compose a dynamic mySQL query statement for inserting a preset  

for(var i=0;i<taskInfo.presetList.length;i++){

var insert_preset_statement =MySQLHelper.composeQuery('preset_insert',

                             [  taskInfo.presetList[i].Name,

                                taskInfo.presetList[i].Category,

                                taskInfo.presetList[i].GUID

                               ]);

this.connection.query(insert_preset_statement, function(err, result) {

if (err) {
         console.log('db error:' + err);
         }
else     {
         console.log('db task : Successfully Added');
         }
     });    
   }
}

JSON:

{
"presetList": [
    {
        "Name": "AARAMBAMTestfile",
        "Category": "Harmonic",
        "GUID": "ABC10203"
    },
    {
        "Name": "ENDHIRANTestfile",
        "Category": "Harmonic",
        "GUID": "DRU03472"
    }
]

}

To do this, I have written a normal insert stored procedure in MySql and it is given below:

MySQL SP:

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `preset_insert`(
IN p_Name  varchar(50),
IN p_Category varchar(50),
IN p_GUID varchar(50)   
) 
BEGIN

INSERT INTO Preset
(
    PresetName,
    PresetCategory,
    PresetGUID,
)
VALUES
(
    p_Name,
    p_Category,
    p_GUID
);

END

The above codes are working fine. But I don't want to insert one by one using for loop. But instead of doing this thing using for loop, I want to use insert select statement for bulk insert at a single time. How to do this using node.js?

2 Answers 2

1

you can make a single insert query like the following, and call the query to execute.

    function insertValues(ob) {
       return "('" + ob.Name + "','" + ob.Category + "','" + ob.GUID + ")";
    }

    var queryStr = "INSERT INTO `Preset` (`PresetName ` ,`PresetCategory `, `PresetGUID `) 
    VALUES ";
    var valueStrs = [];
    for(var i=0;i<taskInfo.presetList.length;i++){
       valueStrs.push(insertValues(taskInfo.presetList[i]));
    }
    queryStr += valueStrs.join(',');

    this.connection.query(queryStr, function(err, result) {

    if (err) {
         console.log('db error:' + err);
    }
    else {
         console.log('db task : Successfully Added');
    }
    });   
Sign up to request clarification or add additional context in comments.

Comments

0

I tried this code and is working fine:

var PresetData = [taskInfo.presetList.length];
for( var i = 0; i < taskInfo.presetList.length; i++){
   PresetData[i] = [ taskInfo.presetList[i].Name, "" + taskInfo.presetList[i].Category, "" + taskInfo.presetList[i].GUID ];
}

console.log("PresetData: " + JSON.stringify(PresetData));
this.connection.query( "INSERT INTO Preset (PresetName, PresetCategory, PresetGUID) VALUES ?", [PresetData], function(err, result) {
});

1 Comment

Hey thevan. I have an array of JSON objects.[{uid: '', name: ''}, {uid: '', name: ''}]. Do you have any idea of how to bulk insert this into mysql using nodejs? Nandri!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.