如何为 Phonegap 移动应用程序创建离线数据库?

How to create offline database for Phonegap mobile application?

我正在为 Android 创建 phonegap 移动应用程序,我需要使用应用程序以离线模式存储问题。我可以为此使用 JSON 吗?如果是,请指导我如何将我的问题存储在单独的文本文件中,以及如何从容器文本文件访问我的应用程序中的特定问题。

您可以使用 localStorage 将您的问题存储为对象数组,如下所示:

questions = [
   { id : 1, text: "Question 1 text goes here" },
   { id : 2, text: "Question 2 text goes here" },
   { id : 3, text: "Question 3 text goes here" },
   { id : 4, text: "Question 4 text goes here" }
];

window.localStorage.setItem("questions" , JSON.stringify(questions));

然后使用 JSON.parse();

读回
questions = JSON.parse( window.localStorage.getItem("questions"));
console.log(questions);

P.S。存储在 localStorage 中的数据不会过期并在您的 phonegap 应用程序关闭后保留。

您也可以使用 android sqlite,您可以为此找到 phonegap/cordova 插件。 Sqlite 是一个轻量级的 RDBMS。

如果您在设置时遇到任何问题,请告诉我。

阿比吉特奈克 www.pattypets.com

您可以使用WebSql 数据库。它不需要任何额外的插件来安装 Phonegap 应用程序。 http://docs.phonegap.com/en/2.9.0/cordova_storage_storage.md.html#Storage

WebSql 数据库的数据库大小限制为 50MB。所以如果你想要更高的存储容量,你可以选择Sqlite数据库。要在 Phonegap 应用程序中使用 Sqlite 数据库,您需要 Sqlite 插件。 https://github.com/litehelpers/Cordova-sqlite-storage