簡體   English   中英

如何使用 mysljs 在 mySql 和 node.js 中批量插入

[英]How to bulk insert in mySql and node.js using mysljs

我無法使用 node.js lib mysljs在我的數據庫中使用批量插入。

我遵循以下答案:

如何使用 node.js 在 mySQL 中進行批量插入

沒有成功。

var sql = "INSERT INTO resources (resource_container_id, name, title, extension, mime_type, size) VALUES ?";

var values = [
  [1, 'pic1', 'title1', '.png', 'image/png', 500], 
  [1, 'pic2', 'title2', '.png', 'image/png', 700]];

return connection.query(sql, [values], (result) => {
    if (err) throw err;
    connection.end();
});

我不斷收到錯誤:

 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near \'?\' at line 1' 

我還嘗試使用 bluebird 來保證查詢方法,但沒有成功,我再次遇到相同的錯誤。

你需要用重音符(反引號)來標記你的鍵,像這樣:`key`

像這樣進行query

var sql = "INSERT INTO resources (`resource_container_id`, `name`, `title`, `extension`, `mime_type`, `size`) VALUES ?";

嘗試在問號周圍使用 ()

var sql = "INSERT INTO resources (resource_container_id, name, title, extension, mime_type, size) VALUES (?) ";

我只有在使用connection.execute()時才會遇到同樣的問題,然后實際上是因為它沒有為准備好的語句實現。

我通過使用connection.query()解決了這個問題。

不知道這個答案是否對任何人有幫助。

當我尋找答案時,它會幫助我。

嘗試刪除values周圍的方括號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM