使用node.js保存数据库中的xml文件文本

我试图保存xmlstring到MySQL数据库这里是我的代码: – 我正在使用MySQL模块pipe理数据库;

//I am reading the xml file from path using this code var stream = fs.createReadStream(path); stream.setEncoding('utf8'); stream.on('data', function (chunk) { var text=chunk.toString('utf8'); //here is the error ie invalid syntax in MySQL query con.query('insert into table_name(text) values("'+text+'")',function(err,data){ //allways getting error. }); }); 

我知道错误是因为文本variables中的字符无效。 所以,我怎么可以连接或逃避的string,请帮助我。

谢谢

从这样的参数构build您的查询:

  con.query('insert into table_name(text) values(?)', [text], function(err,data){ //... }) 

这样,文本被mysql客户端库自动转义