6

I want to insert a 2d array (if we consider it as a table) and it can have any number of rows but a fixed umber of defined columns so how do insert this into a mysql table

var myArr = [
  [ 10000, '2021-03-08', 10000, '2021-03-07', 0, 364 ],
  [ 10000, '2021-04-08', 10000,'2021-03-08', 0, 333 ],
  [ 10000, '2021-05-08', 5000, '2021-03-08', 2500, 303 ]
]

sql = 'INSERT INTO vm001 (Amount, date, entryamt, entrydate, balanceamt, daysdel) VALUES ?'

db.query(sql , myArr , function (err) {
   console.log(err)
})

this is what I tried but it didn't work so can anyone suggest a way to make this work this is the console

 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '10000, '2021-03-08', 10000, '2022-03-07', 0, 364' at line 1

1 Answer 1

6

the problem was a simple square bracket

db.query(sql , [myArr] , function (err) {
   console.log(err)
})

this solves the problem

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

Comments

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.