Phonegap Storage

2019-06-13 15:29发布

问题:

Introduction

I've spend all day but I just don't understand whats happening in my phonegap aplication.

In my app im using the local storage from phonegap, to store news messages retrieved from a web page. I'm doing this because I want the users to be able to watch the content off line.

When they are on line the app will simply sent its last update date to the website, the website will only sent the changed records back and the app stores it in the local db again.

This all works pretty good, exept when loading the data for the first time.

Problem

When I start the app for the very first time, it needs to fill the database with all records available on the website. (201 rows) But when I try to load more then 39 messages, it will create a undefined sql error.

However, I can change the php script so it will only sent 39 results. After the app has loaded for the first time I can change the script again so it will sent all records, and it will work flawless.

To make the problem even more complicated, on every startup of my app all tables are dropped and recreated (of course this is only for development purposes, and the drop will not be included in the final version). After a restart of the app it will load the 201 records without any problem.

To reproduce the problem again, I have to completely uninstall the app, and then reinstall it.

Code

Code for storing the results:

var q = 'INSERT OR REPLACE INTO NIEUWS (id, title, introtext, catid) VALUES (?, ?, ?, ?)';
var len = contents.length;
for (var i=0; i<len; i++){
    tx.executeSql(q, [contents[i].id, contents[i].title, contents[i].introtext, contents[i].catid]);
}

Code on startup:

tx.executeSql('DROP TABLE IF EXISTS NIEUWS');
tx.executeSql('CREATE TABLE IF NOT EXISTS NIEUWS (id PRIMARY KEY, title TEXT, introtext TEXT, catid INTEGER)');

I really hope this problem makes sense to some of you, because I am completely lost right now.

Thanks in advance!

Edit 1 Just tried to only save the id and title, this works without any problem.

var q = 'INSERT OR REPLACE INTO NIEUWS (id, title) VALUES (?, ?)';
var len = contents.length;
for (var i=0; i<len; i++){
    tx.executeSql(q, [contents[i].id, contents[i].title]);
}

Edit 2 Rewritten the code to wait for the callback, the same error still exists.

function HandleNewsQuery(tx){
    var q = 'INSERT OR REPLACE INTO NIEUWS (id, title, introtext, catid) VALUES (?, ?, ?, ?)';
    if(c < contents.length){
        tx.executeSql(q, [contents[c].id, contents[c].title, contents[c].introtext, contents[c].catid], HandleNewsQuery, errorCB);
        c++;
     }
}

回答1:

I think you have to make following SQL statement as a callback of previous one, instead of looping them through with 'for'.

Can't provide an example right now, sorry.



回答2:

After three days of looking into this problem, I finally found the solution. My database was not declared big enough:

var db = window.openDatabase("test", "1.0", "Test DB", 200000);

The size is declared in bytes, so actually this database was only 200KB

I changed this to:

var db = window.openDatabase("test", "1.0", "Test DB", 20000000);

Which is 20MB.

I still don't understand why the problem only existed on the first load of data.

Thanks for all the thinking!