【发布时间】:2022-01-20 03:51:19
【问题描述】:
nodejs中出现sql语法错误
我正在使用此代码:
async function getNotificationFsql(data) {
var query = "select * from notifications where id>${data.start} and receiver=${data.recId} limit 10";
var record = await db.sequelize.query(query ,
function (err,results) {
if (err) {
console.log('Error while getting data from MYSQL :- ' + err);
} else {
console.log('Notifications data which we got from MYSQL :- ' + results);
}
}
);
// console.log(record);
console.log('Notifications :- ' + record[0]);
return record[0];
};
我得到了:
代码:'ER_PARSE_ERROR', 错误号:1064, sqlState: '42000',
sqlMessage:“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,了解在 '{data.start} 和 receiver=${data.recId} limit 10' 附近使用的正确语法在第 1 行",
sql: 'select * from notifications where id>${data.start} and receiver=${data.recId} limit 10', 参数:未定义 }, sql: 'select * from notifications where id>${data.start} and receiver=${data.recId} limit 10', 参数:未定义
我做错了什么?
【问题讨论】:
-
你没有用明确的值替换占位符。
-
不要使用字符串替换构建查询,使用参数化查询。
标签: javascript mysql node.js macos