2

I am using SQL.js for SQLite in my chrome app , I am loading external db file to perform query , now i want to save my changes to local storage to make it persistent , it is already define here-

https://github.com/kripken/sql.js/wiki/Persisting-a-Modified-Database

i am using the same way as defined in the article-

function toBinString (arr) {
    var uarr = new Uint8Array(arr);
    var strings = [], chunksize = 0xffff;
    // There is a maximum stack size. We cannot call String.fromCharCode with as many arguments as we want
    for (var i=0; i*chunksize < uarr.length; i++){
        strings.push(String.fromCharCode.apply(null, uarr.subarray(i*chunksize, (i+1)*chunksize)));
    }
    return strings.join('');
}

function toBinArray (str) {
    var l = str.length,
            arr = new Uint8Array(l);
    for (var i=0; i<l; i++) arr[i] = str.charCodeAt(i);
    return arr;
}

save data to storage -

var data =toBinString(db.export());
chrome.storage.local.set({"localDB":data});

and to get data from storage-

chrome.storage.local.get('localDB', function(res) {
    var data = toBinArray(res.localDB);
      //sample example usage
      db = new SQL.Database(data);
      var result = db.exec("SELECT * FROM user");
});

Now when i make a query , i am getting this error -

Error: file is encrypted or is not a database

is there any differnces for storing values in chrome.storage and localStorage ? because its working fine using localStorage, find the working example here-

http://kripken.github.io/sql.js/examples/persistent.html

as document sugggested here - https://developer.chrome.com/apps/storage

we don't need to use stringify and parse in chrome.storage API unlike localStorage, we can directly saves object and array.

when i try to save result return from db.export without any conversion, i am getting this error-

Cannot serialize value to JSON

So please help me guys what will be the approach to save db export in chrome's storage, is there anything i am doing wrong?

Ahtisham
  • 216
  • 2
  • 13

0 Answers0