I am trying to implement a cache using sqlite in javascript. I have a json object that I am trying to inject into a string and inject into a database, but keep getting a syntax error.
the table consists of two fields: line md5 and line json, as I define db and the table
db = window.openDatabase("Database", "1.0", "Cordova Demo",10485760);
db.transaction(function(tx){
tx.executeSql('DROP TABLE IF EXISTS CACHE_DATA');
tx.executeSql('CREATE TABLE IF NOT EXISTS CACHE_DATA (md5 TEXT UNIQUE, data TEXT)');
},function(tx,error){
console.log('tx error: ' + error);
},function(){
console.log('tx success');
});
This is how I try to enter data where the d.data variable is a json object.
var jsonString = JSON.stringify(d.data, null, '');
db.transaction(function(tx){
tx.executeSql('INSERT OR REPLACE INTO CACHE_DATA (md5, data) VALUES ("'+hash+'", "'+jsonString+'")');
},function(tx,error){
console.log(JSON.stringify(tx.message));
console.log(JSON.stringify(error));
},function(){
console.log('tx success');
});
console.log(jsonString);
What will throw and error
08-27 23:19:55.702: E/SQLiteLog(29831): (1) near "networks": syntax error
The actual line I'm passing looks like this:
08-27 23:19:55.652: D/CordovaLog(29831): {"networks":[{"id":"66","name":"Test"}],"expires":1346138396}
I thought this had something to do with the quotes in the json string, but the type of the field is just text, so I'm not sure what could be a syntax error.
Any ideas?