Я хочу обновить новый объект в столбце json в таблице, используя JSON_ARRAY_APPEND. Это работает, когда я использую жесткий код. Но когда я получаю данные из внешнего интерфейса, возникает проблема.
код: 'ER_PARSE_ERROR', номер ошибки: 1064, sqlMessage: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Customer', "Message": 'eqwe', "Time": '9:34 AM'}' AS JSON)) WHERE order_no='Orde' at line 1
, sqlState: '42000', индекс: 0, sql: UPDATE jlk_message_queue SET txn_data = JSON_ARRAY_APPEND(txn_data, '$', CAST('{"MessageId": 5, "UserType": 'Customer', "Message": 'eqwe', "Time": '9:34 AM'}' AS JSON)) WHERE order_no='Order 123'
Это похоже на проблему с одинарными и двойными кавычками. Есть ли способы решить эту проблему? Заранее спасибо
customerModel.sendChatMessage = function (MessageId, UserType, Message, Time, OrderNo, result) {
var sql = "UPDATE jlk_message_queue " +
"SET txn_data = JSON_ARRAY_APPEND(txn_data, '$', " +
"CAST('{" + '"MessageId": ?, "UserType": ?, "Message": ?, "Time": ?' + "}' AS JSON)) " +
"WHERE order_no=? ";
// ********Sample data which could work*********
// UPDATE jlk_message_queue
// SET txn_data = JSON_ARRAY_APPEND(txn_data, '$', CAST('{"MessageId": 1, "UserType": "Doctor", "Message": "Hi, This is doctor X, May I help you?", "Time": "12:30 PM"}' AS JSON))
// WHERE order_no='Order 123'
pool.getConnection(function (err, con) {
if (err) throw err;
con.query(sql, [MessageId, UserType, Message, Time, OrderNo], function (err, res) {
if (err) {
con.destroy();
result(err, null);
}
else {
console.log(res);
if (res.affectedRows == 1) {
result(null, { result: true });
}
else {
result(null, { result: true, value: 'Fail to send message' });
}
}
});
});
};