我想在每个新用户注册时用 CURRENT_TIMESTAMP
INSERT
当前时间戳。列号等于 VALUES
中参数的数量。然而我得到 INSERT has more target columns than expressions
。使用 node-postgres
npm 模块作为 Controller 。
//Just 3 parameters, timestamp is hardcoded in the query
exports.create = function (username, email, password) {
DB.connect(connection, function (err, client, done) {
var query = client.query(
//4 columns
"INSERT INTO users (username, email, userpass, datecreated) VALUES" +
//4 parameters
"(" + "'" + username + "'" + "," + "'" + email + "'" + "," + "'" + password + "'" + "'CURRENT_TIMESTAMP')");
query.on('error', function (error) {
console.log("query returned an " + error);
});
query.on('row', function (row, result) {
result.addRow(row);
});
});
};
最佳答案
密码后缺少逗号且 current_Timestamp 周围没有任何符号
"INSERT INTO users (username, email, userpass, datecreated) VALUES" +
//4 parameters
"(" + "'" + username + "'" + "," + "'" + email + "'" + "," + "'" + password + "," + "'CURRENT_TIMESTAMP)"
--- 虽然这可能是解决当前问题的公认答案,但我强烈建议对 Craig 和 Lars 的答案进行评估。使用参数是一种更好的长期方法,因为它更安全;一旦您了解了如何以及正确的现代范例,实际上更容易编码。
关于javascript - Node JS+PostgreSQL : INSERT has more target columns than expressions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37679959/