How to insert json object into sqlite table in javascript, phonegap (cordova)

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?

+5
source share
4 answers

, JSON ,

            var jsonString = escape(JSON.stringify(d.data));

            db.transaction(function(tx){
                tx.executeSql('INSERT OR REPLACE INTO CACHE_DATA (md5, json, expires) VALUES ("'+hash+'", "'+jsonString+'","'+expireStamp+'")');
            },function(tx,error){
                console.log(JSON.stringify(tx.message));
                console.log(JSON.stringify(error));
            },function(){

                });
+7

:

var valuesInArray = [hash, JSON.stringify(d.data, null, "");

tx.executeSql("INSERT OR REPLACE INTO CACHE_DATA (md5, data) VALUES (?, ?)", valuesInArray, function(tx, result)  {
  // Success Callback
});
+2

@

- json sqlite?

  • json URL- . URL . URL- sqlite, cordova?

,

begginer

, - -. angular 2

0

encodeURI() decodeURI(). :

var jsonString = encodeURI (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');
        });

var data = JSON.parse(decodeURI(sqlData.data));

Note. escape () and unescape () may work. But the function is escape()deprecated in JavaScript version 1.5.

0
source

All Articles