【问题标题】:ER_BAD_FIELD_ERROR: Unknown column in 'field list'ER_BAD_FIELD_ERROR:“字段列表”中的未知列
【发布时间】:2019-12-09 23:48:04
【问题描述】:

我对 sql 不是很感兴趣,正在做我的第一个项目

我读到了来自tutorial points 的更新查询

首先我创建了一个看起来像这样的辅助函数

const updateFieldInTable = (tableName, conditionParameter, updatedCondition, locationReference, locationReferenceValue) => {
  return new Promise((resolve, reject) => {
    pool.getConnection((error, connection) => {
      if (error) return reject(error)
      const query = `UPDATE ${tableName} SET ${conditionParameter} = ${updatedCondition} WHERE ${locationReference} = ${locationReferenceValue}`
      connection.query(query, (error, response) => {
        connection.destroy()
        if (error) return reject(error)
        return resolve(response)
      })
    })
  })
}

我正在使用它来更新表中的字段,因此我创建了一个虚拟路由来为我执行此任务并查看它是否有效

app.get('/test', async (req, res) => {
  const resultFromQuery = await updateFieldInTable('personal', 'gradYear', 2017, 'userId', 1234)
  console.log(`Result from Query:`,  resultFromQuery)
  res.status(200).json(resultFromQuery[0])
});

上面的查询工作得很好,但是如果我把它改成varchar,我会得到以下错误

错误:ER_BAD_FIELD_ERROR:“字段列表”中的未知列“ryan”

这是给我错误的原因

app.get('/test', async (req, res) => {
  const resultFromQuery = await updateFieldInTable('personal', 'school', 'ryan', 'userId', 1234)
  console.log(`Result from Query:`,  resultFromQuery)
  res.status(200).json(resultFromQuery[0])
});

这就是我的 sql 愚蠢的样子

 `gradYear` int(4) DEFAULT NULL,
  `gradMonth` enum('january','february','march','april','may','june','july','august','september','october','november','december') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
  `school` varchar(255) DEFAULT NULL,
  `degree` varchar(255) DEFAULT NULL,

【问题讨论】:

    标签: javascript mysql node.js


    【解决方案1】:

    当您想在 db 上插入/更新字符串值时,它应该在单引号之间。用你的参数 sql 看起来像 SET school = ryan 但应该是 SET school = 'ryan'。 因此,将 ryan 值发送到您的函数,例如 '\'ryan\''"'ryan'"

    【讨论】:

      猜你喜欢
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      • 2012-11-09
      • 2020-03-29
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多