简体   繁体   中英

How to insert multiple records/rows in MySQL

Is it possible in Sails.js to insert multiple records? I am using MySQL, I need to save 'n' rows into MySQL depending upon request coming from API.

I need help with respect to Sails.js Thank you

Yes you can write multiple records in mysql at once. let my model is:

module.exports = {
  tableName:'test',
  autoCreatedAt:false,
  autoUpdatedAt:false,
  attributes: {
    name:'string'
  }
};

First let's see how is it done in mysql query.

INSERT INTO `test` VALUES (1,'AB'),(2,'ABCCDC');

Now this is how you can add multiple records...

Test.create([{name:'name1'},{name:'name2'},{name:'name3'}])
  .exec(function(err,done){
    if(err){
      //hendle the error;
      return;
    }
    //successfully inserted;
  });

Now i guess my point is clear.

you just have to pass the array of objects in model.create() to insert multiple objects.

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