lundi 10 août 2015

SQLite insert into table, with JSON.stringyfy. Breaks with Quoted String.

I am using SQLite for mobile, mostly for hybrid mobile app development.

I am storing data to in one column like this.

var QueryToInsert = JSON.stringify(object) // where object is defined as below

INSERT OR REPLACE INTO Table (id,data) VALUES ('1', " ' " + QueryToInsert + " ' " );

which becomes to

INSERT OR REPLACE INTO Table (id,data) VALUES ('1','[{"name":"Romeo","lastName":"Juliet"}]')

While this works , but if i have a object like this:

var object = {"name": "In connection with this appellative of 'Whalebone Whales' , it is of great..." }  

Note the '' in the string.

Now if I do.

 INSERT OR REPLACE INTO Table (id,data) VALUES ('1', " ' " + JSON.stringify(object) + " ' " );

It gives me an error :

{"code":5,"message":"near \"Whalebone\": syntax error"}

Solution I have tried :

replace all the ' with some character before making the query. It Does work, but when there is lot of data and lot of tables, replacing all ' its a slow process.

I am looking for any solution that will help me with this.

Aucun commentaire:

Enregistrer un commentaire