最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ER_BAD_FIELD_ERROR: Unknown column in 'field list' - Stack Overflow

programmeradmin4浏览0评论

I am not that much into sql and was working on my first project

I read about updating queries from tutorial points

To start with I created a helper function which looks like this

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)
      })
    })
  })
}

I am using this to update field in my table, So I create a dummy route to perform this task for me and to see if this works or not

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])
});

The above query works perfectly fine, but If i change it to a varchar, I am getting the following error

Error: ER_BAD_FIELD_ERROR: Unknown column 'ryan' in 'field list'

Here is what is giving me the error

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])
});

This is how my sql dumb for the same would look like

 `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,

I am not that much into sql and was working on my first project

I read about updating queries from tutorial points

To start with I created a helper function which looks like this

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)
      })
    })
  })
}

I am using this to update field in my table, So I create a dummy route to perform this task for me and to see if this works or not

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])
});

The above query works perfectly fine, but If i change it to a varchar, I am getting the following error

Error: ER_BAD_FIELD_ERROR: Unknown column 'ryan' in 'field list'

Here is what is giving me the error

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])
});

This is how my sql dumb for the same would look like

 `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,
Share Improve this question asked Aug 1, 2019 at 11:29 AlwaysblueAlwaysblue 12k44 gold badges141 silver badges253 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

When you want to insert/update string value on db it should be between single quotes. With your parameters sql seems like SET school = ryan but it should be SET school = 'ryan'. So send ryan value to your function like '\'ryan\'' or "'ryan'"

发布评论

评论列表(0)

  1. 暂无评论