database Phonegap如何在android上保存永久数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13761707/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Phonegap how to save permanent data on android
提问by Daniel
I'm looking for a method to save data permanently using PhoneGap.
我正在寻找一种使用PhoneGap.
- LocalStorage
- File I/O
- ?
- LocalStorage
- 文件输入/输出
- ?
I heard LocalStorageis not 100% permanent, but is somewhere in the alley.
我听说LocalStorage不是 100% 永久的,而是在胡同的某个地方。
I wouldn't really like to begin with File I/O, because what I need is to create a database with recipes, and they need to stay.
我真的不想从文件 I/O 开始,因为我需要的是创建一个包含配方的数据库,并且它们需要保留。
"Number 3" is what I'm asking about. Any suggestions?
“3号”就是我要问的。有什么建议?
Thanks in advance :)
提前致谢 :)
回答by A. Magalh?es
I'm using the localstorage and this is the best solution to keep saved data. Like you said, it isn't 100% permanent. In my case the localstorage is cleared only if you uninstall the app. Find more info about localstorage with Phonegap http://docs.phonegap.com/en/2.1.0/cordova_storage_storage.md.html#localStorage
我正在使用 localstorage,这是保留已保存数据的最佳解决方案。就像你说的,它不是100%永久的。在我的情况下,只有在卸载应用程序时才会清除本地存储。使用 Phonegap 查找有关 localstorage 的更多信息 http://docs.phonegap.com/en/2.1.0/cordova_storage_storage.md.html#localStorage
You could get more info about localstorage on http://diveintohtml5.info/storage.html
您可以在http://diveintohtml5.info/storage.html上获得有关 localstorage 的更多信息
You could use a database as well. You can find info on this at http://docs.phonegap.com/en/2.1.0/cordova_storage_storage.md.html#openDatabase
您也可以使用数据库。您可以在http://docs.phonegap.com/en/2.1.0/cordova_storage_storage.md.html#openDatabase上找到相关信息
回答by Alex
If you would like to store the data on the device permanently even after the app in uninstalled, you will have to do it in a different way on iOS and android.
如果您想在卸载应用程序后永久存储设备上的数据,则必须在 iOS 和 android 上以不同的方式执行此操作。
On iOSyou can use the keychain https://github.com/shazron/KeychainPlugin.
在iOS 上,您可以使用钥匙串https://github.com/shazron/KeychainPlugin。
On Androidyou can use the file plugin, and store the data on the SD card (cordova.file.externalRootDirectory).
在Android 上,您可以使用文件插件,并将数据存储在 SD 卡 (cordova.file.externalRootDirectory) 上。
In both cases, the data will remain on the device even if the user re-installs the app.
在这两种情况下,即使用户重新安装应用程序,数据也会保留在设备上。
If you are using ionic (or just angular), you can use ng-persistthat wraps these two plugins in an angular service with the same API for both ios and android.
如果您正在使用 ionic(或只是 angular),您可以使用ng-persist将这两个插件包装在一个 angular 服务中,并为 ios 和 android 提供相同的 API。
More details here http://alexdisler.com/2014/04/23/persist-data-mobile-device-app-removed-uninstalled-cordova-ionic/
更多细节在这里http://alexdisler.com/2014/04/23/persist-data-mobile-device-app-removed-uninstalled-cordova-ionic/
回答by Shahrukh Anwar
You could do the following steps for saving any data permanently,
您可以执行以下步骤来永久保存任何数据,
- Your HTML file - <div id="deviceready">- <div id="result"></div>- </div>
- You JS file - window.onload = function(){ document.addEventListener("deviceready", init, false); } function init(){ document.getElementById("signin").addEventListener("submit",login, false); } function saveData(){ window.localStorage.setItem("header", 'Some Text Here'); } function getData(){ document.getElementById("result").innerHTML = window.localStorage.getItem("header"); } }
- 你的 HTML 文件 - <div id="deviceready">- <div id="result"></div>- </div>
- 你的JS文件 - window.onload = function(){ document.addEventListener("deviceready", init, false); } function init(){ document.getElementById("signin").addEventListener("submit",login, false); } function saveData(){ window.localStorage.setItem("header", 'Some Text Here'); } function getData(){ document.getElementById("result").innerHTML = window.localStorage.getItem("header"); } }
回答by Vatanay ?zbeyli
In AppStore, when you release new version of your app, all of stored datas on your users will be reset. Before you update your app, you need to find better way to store your data.
在 AppStore 中,当您发布新版本的应用程序时,您的用户存储的所有数据都将被重置。在更新您的应用程序之前,您需要找到更好的方式来存储您的数据。

