如何将一个二维JavaScript数组插入到MySQL表中。

6

我想要插入一个二维数组(如果我们将其视为表格),它可以有任意数量的行,但有一定数量的定义列,那么如何将其插入到MySQL表中?

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

这是我尝试过的方法,但它并没有起作用,有人能建议一种让它起作用的方法吗?这是控制台。
 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个回答

6
问题出在一个简单的方括号上。
db.query(sql , [myArr] , function (err) {
   console.log(err)
})

这解决了问题。

myArr to [myArr]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接