简体   繁体   English

如何用空格将单引号从javascript替换为mysql

[英]How to replace single quote by space from javascript to mysql

I am currently working on javascript (with Hapijs). 我目前正在使用javascript(使用Hapijs)。 My main mission in my company is to read data in mongodb, then feed it back into an mysql database. 我公司的主要任务是读取mongodb中的数据,然后将其反馈到mysql数据库中。

My problem occurs when data in the mongodb collection contains single-quotes ('). 当mongodb集合中的数据包含单引号(')时,会发生我的问题。 For example, I have a data "O'Neill" and i have to put it in mysql. 例如,我有一个数据“ O'Neill”,我必须将其放在mysql中。 I found an escape() function but it replaces the single quote with "%" followed by a hexadecimal number. 我找到了一个escape()函数,但是它将单引号替换为“%”,后跟一个十六进制数字。

I would like, if possible, to keep the single quote. 如果可能的话,我希望保留单引号。 Otherwise, replace it with a space. 否则,将其替换为空格。

Do you have any ideas ? 你有什么想法 ?

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;
    });
});

Here is a part of my code. 这是我的代码的一部分。 People with single-quote wont be added to mysql database. 单引号的人不会被添加到mysql数据库中。

Try Below code 尝试以下代码

const sql = "INSERT INTO 2018_ACCOUNTS (ID, LOGIN, FIRST_NAME, LAST_NAME, DATE, EVENT, UNIK) VALUES (?,?,?,?,?,?,?)";
    con.query(sql,[account._id ,account.login ,account.name, account.name2, dateMySQL ,account.evt_id ,account.unik], function (err, result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM