【发布时间】: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