【发布时间】:2018-09-11 00:52:47
【问题描述】:
我一直在尝试为使用 Node js 和 npm 包 pg 的某些用户准备一个查询来更新我的表中的行。当我运行查询时出现问题,我的错误消息将返回错误:
“VALUES”或“WHERE”处或附近的语法错误
但从不提供更多关于错误的上下文,从而难以确定。我曾尝试重新排列值并尝试不同的方法,但均未成功。
populateProfileData(username, gender, dob, country, province, city, date, user_id) {
console.log('================' + username + gender + dob + country + province + city + date + user_id);
return new Promise(function (resolve, reject) {
const client = new pg.Client(connectionString);
client.connect()
.then(() => console.log('connected'))
.catch(err => console.error('connection error', err.stack))
// This Works ---> UPDATE users SET dob = '032018' WHERE USER_id = 15
const text = 'UPDATE users SET(username, dob, country, province, city, date) WHERE user_id = (user_id) VALUES($1, $2, $3, $4, $5, $6, $7)';
const values = [username, dob, country, province, city, date, user_id]
const query = client.query(text, values)
.then(success => {
console.log('ran query')
resolve(success);
})
.catch(error => {
console.log('--------' + error)
reject(error);
})
});
}
}
【问题讨论】:
-
您的查询语法有问题,即
WHERE user_id = (user_id)不代表任何有意义的东西。
标签: node.js database postgresql pg