我目前正在使用javascript(使用Hapijs)。我在公司的主要任务是读取mongodb中的数据,然后将其反馈到mysql数据库中。
当mongodb集合中的数据包含单引号(')时,我的问题就出现了。例如,我有一个数据" O' Neill"我必须把它放在mysql中。我找到了一个escape()函数,但它用"%"替换了单引号。后跟一个十六进制数字。
如果可能的话,我想保留单引号。否则,请用空格替换它。
你有什么想法吗?
谢谢!
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;
});
});
这是我的代码的一部分。单引号的人不会被添加到mysql数据库。
答案 0 :(得分:1)
尝试以下代码
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)