简体   繁体   中英

Bulk insert in MySql using node.js

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?

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');
    }
    });   

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) {
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM