I'm currently focused on JavaScript using Hapi.js in my role at the company. My main task involves retrieving data from MongoDB and transferring it to a MySQL database.
The challenge arises when dealing with data that contains single quotes within the MongoDB collection. For instance, if I have a record like "O'Neill", inserting it into MySQL becomes tricky. While I came across the escape() function, it replaces the single quote with "%" followed by a hexadecimal number.
If feasible, I would prefer to retain the single quote; otherwise, replacing it with a space is acceptable.
Do you have any suggestions or solutions for this issue?
Thank you!
con.connect(function(err) {
if (err) throw err;
accounts.forEach((account) => {
const sql = "INSERT INTO 2018_ACCOUNTS (ID, LOGIN, FIRST_NAME, LAST_NAME, DATE, EVENT, UNIK) VALUES ('" + account._id + "', '" + account.login
+ "', '" + account.name + "', '" + account.name2 + "', '" + dateMySQL + "', " + account.evt_id + ", '" + account.unik + "')";
con.query(sql, function (err, result) {
if (err) throw err;
});
});
This snippet of code illustrates the problem where individuals with single quotes in their names are not being added to the MySQL database.