简体   繁体   English

Phonegap本地存储返回空结果

[英]Phonegap local storage returns empty result

On app's first load, I retrieve data from storage (Phonegap's WebSQL storage type). 在应用的首次加载时,我从存储(Phonegap的WebSQL存储类型)中检索数据。 Phonegap storage results load fine. Phonegap存储结果加载良好。 Until... 直到...

When I go to another page in the app: <a href="page2.html" rel="external">Link</a> 当我转到应用程序中的另一个页面时: <a href="page2.html" rel="external">Link</a>

If I return back to that main page from the second page using window.location.href in Javascript, I retrieve the WebSQL data again (same function as before). 如果我使用Javascript中的window.location.href从第二页返回该主页,则会再次检索WebSQL数据(与以前的功能相同)。

Phonegap Storage calls the success function (no error), but with an empty result set . Phonegap Storage调用成功函数(无错误),但结果集 It seems to be loading from a second database where I haven't stored anything into (see edit below). 它似乎是从第二个我没有存储任何内容的数据库加载的(请参见下面的编辑)。

If I force quit app and reopen, storage loads fine again, which shows me that the results in the DB are NOT being deleted. 如果我强制退出应用程序并重新打开,存储将再次正常加载,这表明数据库中的结果并未被删除。

I load the storage after ondeviceready is fired. 我在ondeviceready后加载了存储。 What could be wrong? 有什么事吗

Note: 注意:
1. This is not happening on simulators. 1.这不是在模拟器上发生的。 only happening on real Android 4.0 device. 仅在真正的Android 4.0设备上发生。
2. This app uses jQuery / jQuery Mobile. 2.此应用程序使用jQuery / jQuery Mobile。

function ondeviceready() {
   db = window.openDatabase("test", "1.0", "test DB", 2000000);
   // . . . //
   db.transaction(function(tx) {
      tx.executeSql('SELECT * FROM table ORDER BY name', [], querysuccess, function(tx, e) {
         errorAlert();
      });
   }), errorCB;
}

EDIT : I'm noticing now that if I add a new row when app is first loaded, it stores in one database. 编辑 :我现在注意到,如果我在首次加载应用程序时添加新行,它将存储在一个数据库中。 Then, if I go to the second page and then back to the first page, and I add another row, it also stores, but in a separate database! 然后,如果我转到第二页,然后返回第一页,然后添加另一行,它也将存储,但是存储在单独的数据库中! (so that's what it seems to be doing). (这就是它的作用)。 So strange, I see this happening. 真奇怪,我看到了这种情况。 All my rows are saved and persistent, but the query returns a different group of results depending on whether I went to the second page or not... 我所有的行都已保存并保持不变,但是查询会返回另一组结果,具体取决于是否转到第二页...

Also: The second page is having a jQuery error for some reason. 另外:由于某些原因,第二页出现jQuery错误。 I just created a blank page with a script link to jQuery, and there is an error. 我刚刚创建了一个空白页面,并带有指向jQuery的脚本链接,但出现错误。 Strange... I wonder if this error is affecting the database? 奇怪...我想知道这个错误是否正在影响数据库? I'm trying to discover how to solve this error. 我试图发现如何解决此错误。

I've encountered an issue similar to this one before, but my problem was that I was trying to write to the same database from the Java side and the Javascript side. 我之前遇到过类似的问题,但是我的问题是我试图从Java端和Javascript端写入同一数据库。 This is how I was able to solve it: 这就是我能够解决的方法:

  • First I ran the app on an Android Simulator and used DDMS to access the databases and that is when I realised that the two were writing in different databases. 首先,我在Android Simulator上运行了该应用程序,并使用DDMS来访问数据库,也就是当我意识到两者在不同的数据库中编写时。

  • I changed from using WebSQL , to using proper SQLite database, and I started getting consistent results after that. 我从使用WebSQL更改为使用适当的SQLite数据库,此后我开始获得一致的结果。 It's a drop-in replacement for the phonegap's implementation of database storage, so no need to change your code much. 它是phonegap数据库存储实现的直接替代,因此无需太多更改代码。 Please see https://github.com/brodyspark/PhoneGap-SQLitePlugin-Android . 请参阅https://github.com/brodyspark/PhoneGap-SQLitePlugin-Android

I think it's also important to note that WebSQL has some storage limitations, so you might be better off using SQLite that WebSQL . 我认为还必须注意WebSQL具有一些存储限制,因此使用SQLite不是WebSQL可能会更好。 Please see: http://www.steveworkman.com/html5-2/standards/2011/the-limitations-of-websql-and-offline-apps/ 请参阅: http : //www.steveworkman.com/html5-2/standards/2011/the-limitations-of-websql-and-offline-apps/

Hope this helps :-) 希望这可以帮助 :-)

What I've done is I now load the external HTML page through Ajax: 我要做的是现在通过Ajax加载外部HTML页面:

$.mobile.changePage("page2.html");

I also switched to pgsqlite as recommended. 我还根据建议切换到了pgsqlite。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM