簡體   English   中英

INSERT INTO 語句在使用 mySQL 的 NodeJS 中不起作用

[英]INSERT INTO statement not working in NodeJS using mySQL

我一直在用頭撞牆,試圖找出我所擁有的 mySQL 語法的問題。 我收到錯誤:

{"message":"您的 SQL 語法有錯誤;請查看與您的 MySQL 服務器版本相對應的手冊,以了解在 '?, ?, ?,?,?,?,(SELECT account_id FROM accounts WHERE account_name = ?),?,?,?,UUI' 在第 1 行"}

這是我設置的后端代碼:

const Customer = function(customer) {
  this.customer_id = customer.customer_id;
  this.contact_title = customer.contact_title;
  this.contact_first = customer.contact_first;
  this.contact_last = customer.contact_last;
  this.referral_source = customer.referral_source;
  this.initial_contact_date = customer.initial_contact_date;
  this.job_title = customer.job_title;
  this.account = customer.account;
  this.phone = customer.phone;
  this.email = customer.email;
  this.comments = customer.comments;
  this.created_by = customer.created_by;
  this.status = customer.status;
}

Customer.create = (newCustomer, result) => {
  console.log(newCustomer);
  const sql = 'INSERT INTO customers VALUES(UUID_TO_BIN(?, 1),?,?,?,?,?,?,(SELECT account_id FROM accounts WHERE account_name = ?),?,?,?,UUID_TO_BIN(?, 1),?);';
  db.query(sql, newCustomer, (err, res) => {
    if (err) {
      result(err, null);
      return;
    }

    result(null, { id: res.insertId, ...newCustomer });
  });
};

我一生都無法弄清楚錯誤在哪里,因為它在工作台上工作。 任何幫助將不勝感激,謝謝!

其實我自己也找到了答案。 我試圖使用一個對象,認為這些值會被放入語句中。 只需使用 Object.values 創建一個新變量並將其放入數據庫函數中。

暫無
暫無

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

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