用nodeJS讲MongoDB中的数据导入导入mysql的时候出现了一个奇怪的问题。

如题:
我想把MongoDB里面的数据拿到之后,再重新用MySQL的语句insert到数据库,但是报错了,应该是语法里面有一个细节的东西。

nodeJS:

connection.query(`INSERT INTO NEWSLIST(news_title,news_time,author_name,author_image,news_url,news_tag,reptile_time,news_image,content_image,news_content) values("${news_title}","${news_title}","${news_time}","${author_name}","${author_image}","${news_url}","${news_tag}","${reptile_time}","${news_image}","${content_image}","${news_content}")`,

(err,result) \=> {

if(err) throw  err

console.log(result)

})

Error: ER_PARSE_ERROR: 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 '参与支持“反中乱港” “台独”分子李孟居被捕"",""参与支�' at line 1

code: 'ER_PARSE_ERROR',
errno: 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 '参与支持“反中乱港” “台独”分子李孟居被捕"",""参与支�' at ' +'line 1',
sqlState: '4

因为mysql很长时间没用了,不知道怎么处理。

望各位大神不吝赐教。

阅读 1.3k
1 个回答

那是因为的插入的内容里面有双引号没有转义,所以报错了。另外你这种纯手工拼接sql的方式不推荐,很容易遭受sql注入攻击,如果你用Prepared statements之类的东西,就能够一次过解决这两个问题。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进