【问题标题】:INSERT INTO statement not working in NodeJS using mySQL使用 mySQL 在 NodeJS 中插入 INTO 语句不起作用
【发布时间】:2020-09-15 18:30:54
【问题描述】:

我一直在拼命想找出我所拥有的 mySQL 语法的问题。我收到了错误:

{"message":"您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,了解在 '?, ?, ?,?,?,? 附近使用的正确语法,(SELECT account_id FROM accounts WHERE account_name = ?),?,?,?,UUI' at line 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 });
  });
};

我一生都无法弄清楚错误出在哪里,因为它在工作台上有效。任何帮助都会非常感谢,谢谢!

【问题讨论】:

  • 您忘记准备语句并添加参数值。
  • 如果您要插入所有列,您不应该准备语句吗?这些值来自 newCustomer。 db.query 值为 sql = statement, newCustomer = values

标签: javascript mysql sql node.js


【解决方案1】:

其实自己找到了答案。我试图使用一个对象,认为这些值将被放入语句中。只需使用 Object.values 创建一个新变量并将其放入数据库函数中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 2014-06-28
    • 1970-01-01
    • 2011-07-28
    • 2012-08-05
    • 1970-01-01
    相关资源
    最近更新 更多