1

I am trying to save xml string to MySQL database here is my code:- I am using MySQL module for manage database;

   //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 i.e invalid syntax in MySQL query
     con.query('insert into table_name(text) values("'+text+'")',function(err,data){
         //allways getting error.
     });         
   });

I know the error because of invalid characters in text variable. So how I can concatenate or escape the string please help me.

Thanks

1 Answer 1

1

Build your query from parameters like this:

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

this way text is escaped automatically by mysql client library

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.