我对sql不太感兴趣,我在做我的第一个项目
我从tutorial points
首先,我创建了一个helper函数,如下所示

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,就会得到以下错误
错误:“字段列表”中的未知列“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 dumb for the same的样子
 `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,

最佳答案

当您想在数据库上插入/更新字符串值时,它应该在单引号之间。用你的参数sql看起来像
SET school = ryan
但应该是
SET school = 'ryan'
所以把ryan的价值传给你
'\'ryan\''"'ryan'"

07-28 11:41