PhoneGap的存储PhoneGap

2023-09-07 03:53:01 作者:回忆的酒

简介

我已经花了一整天,但我就是不明白什么在我的PhoneGap aplication发生。

在使用从PhoneGap的本地存储器,用于存储从网页中检索新闻消息我的应用程序即时。我这样做是因为我希望用户能观看离线的内容。

当他们上线的应用程序将简单地发送其最后更新日期的网站,该网站将只发送更改的记录背部和应用程序存储在本地数据库一次。

构建跨平台APP HTML5 PhoneGap移动应用实战 带目录完整pdf

这所有的加载数据的第一次工作时pretty好,exept。

问题

当我开始了第一次的应用程序,它需要与网站上提供的所有记录填充数据库。 (201行),但是当我尝试加载更多的则39的消息,它会创建一个未定义的SQL错误。

不过,我可以改变PHP脚本,因此将只派39的结果。应用程序加载的第一次后,我可以再次修改脚本,以便它会发送的所有记录,并且它会工作完美无瑕。

为使问题更加复杂,在我的应用程序每次启动时所有的表,删除并重新创建(当然这仅仅是为发展宗旨,和下降将不会被包含在最终的版本)。应用程序重新启动后,它将装载201记录没有任何问题。

要再次重现该问题,我必须完全卸载该应用程序,然后重新安装。

code

code存储结果:

 变种Q ='插入或更换INTO呼号(ID,标题,introtext,CATID)VALUES(,,,????);VAR LEN = contents.length;对于(VAR I = 0; I< LEN,我++){    tx.executeSql(Q,[内容[I] .ID,内容[I] .title伪,内容[I] .introtext,内容[I] .catid]);} 

在启动code:

  tx.executeSql('DROP TABLE IF EXISTS呼号');tx.executeSql(CREATE TABLE IF NOT EXISTS呼号(ID PRIMARY KEY,标题文字,introtext TEXT,INTEGER CATID)'); 

我真的希望这个问题是有道理的一些你,因为我完全失去了现在。

在此先感谢!

修改1 只是试图既节省id和标题,这工作没​​有任何问题。

 变种Q ='插入或更换INTO呼号(ID,标题)VALUES(?,?)';VAR LEN = contents.length;对于(VAR I = 0; I< LEN,我++){    tx.executeSql(Q,[内容[I] .ID,内容[I] .title伪]);} 

编辑2 改写了code,以等待回调,同样的错误仍然存​​在。

 函数HandleNewsQuery(TX){    变种Q ='插入或更换INTO呼号(ID,标题,introtext,CATID)VALUES(,,,????);    如果(℃下contents.length){        tx.executeSql(Q,[内容[C] .ID,内容[C] .title伪,内容[C] .introtext,内容[C] .catid] HandleNewsQuery,errorCB);        C ++;     }} 

解决方案

在寻找这个问题的三天,我终于找到了解决办法。我的数据库未宣布不够大:

  VAR DB = window.openDatabase(测试,1.0,测试DB,200000); 

的大小以字节为单位申报,所以实际上这个数据库只有200KB

我把它改为:

  VAR DB = window.openDatabase(测试,1.0,测试DB,20000000); 

这是20MB。

我还是不明白,为什么这个问题只在数据的前负荷的存在。

感谢所有的想法!

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++;
     }
}

解决方案

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!