1

I'm not sure what I'm missing, I thought putting Games in brackets would let me do bulk insert to MySQL. However when I do this it comes back as (OBJECT,OBJECT). If I remove the brackets then I only get one single insert but it works! Any suggestions to do bulk insert?

Short sample of the Array

[{title:xxx,link:https://xxxx,description:xxx.,xxx:xxxxx},{title:xxx,link:https://xxxx,description:xxx.,xxx:xxxxx}]

Javascript

// Writing the Games inside a json file
fs.writeFile("feed.json", JSON.stringify(Games), function(err) {
  if (err) throw err;
  console.log("Saved!");
});

 con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  var sqlquery  = "INSERT INTO gtable (title, link, description, company) VALUES ?";
  let queryData = {
    sql: sqlquery,
    values: [Games]
  }
  con.query(queryData, function (err, result) {
    if (err) throw err;
    console.log("Number of records inserted: " + result.affectedRows);
  });
});
1
  • What if you try something like this? So, something like: con.query(sqlquery, [Games], function (err, result) { /* ... */ }) Commented Jul 3, 2020 at 0:03

2 Answers 2

3

I believe your issue may be that you are passing an array of objects while MySQL is expecting an array of arrays.

Your format is basically:

var sqlquery  = "INSERT INTO gtable (title, link, description, company) VALUES ?";
var values = [
[{title:xxx,link:https://xxxx,description:xxx.,xxx:xxxxx}
{title:xxx,link:https://xxxx,description:xxx.,xxx:xxxxx}]
];

When it should be:

var sqlquery  = "INSERT INTO gtable (title, link, description, company) VALUES ?";
var values = [
[[title1,link1,description1,xxx1],
[title2,link2,description2,xxx2]]
];

You would need to convert each object to an array of just its values. Which you could do with something like this I think:

let sqlValues = Games.map(obj => Object.values(obj));

EDIT: I had the incorrect format for the mapping, fixed now.

Sign up to request clarification or add additional context in comments.

Comments

1

I believe @otw has a solid solution (and an awesome explanation of why this is happening), but to offer an alternative solution you could do something like this:

Games.forEach(game => {
  con.query('INSERT INTO gtable SET ?', game, insertErr => {
    if (insertErr) {
      throw new Error("Error inserting game " + game.title + "! " + insertErr);
    }
  
    console.log(`Inserted ${game.title}!`);
  });
})

1 Comment

as an alternative solution, this works well! thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.