我正在为基于 phonegap 的 Android 应用程序使用 SQLLite。我面临的问题是,只要应用程序打开,我就能够获取存储在 local.db 文件中的任何数据。 例如,我有一个设置功能,用户可以在其中保存他/她的设置并将其转到数据库。现在我面临的问题是我无法在应用程序关闭后获取用户保存的数据,我只要应用程序打开就可以获取数据。 所以问题是数据库不是持久的。谁能告诉我如何在应用程序关闭时将数据存储在内存中??
这是我的mainActivity文件
public class Demo extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
super.loadUrl(Config.getStartUrl());
try {
String pName = this.getClass().getPackage().getName();
this.copy("Databases.db", "/data/data/" + pName + "/app_database/");
this.copy("0000000000000001.db", "/data/data/" + pName
+ "/app_database/file__0/");
} catch (IOException e) {
e.printStackTrace();
}
}
void copy(String file, String folder) throws IOException {
File CheckDirectory;
CheckDirectory = new File(folder);
if (!CheckDirectory.exists()) {
CheckDirectory.mkdir();
InputStream in = getApplicationContext().getAssets().open(file);
OutputStream out = new FileOutputStream(folder + file);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);
in.close();
out.close();
}
}
}
这些是我授予的权限:-
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
将复制代码移动到 NigelK 提到的 if 条件后成功了。但是在卸载构建并创建新构建后出现以下错误:-
01-10 16:11:34.466: E/SQLiteLog(29253): (1) no such table: CacheGroups
01-10 16:11:34.466: D/WebKit(29253): ERROR:
01-10 16:11:34.466: D/WebKit(29253): Application Cache Storage: failed to execute statement "DELETE FROM CacheGroups" error "no such table: CacheGroups"
01-10 16:11:34.466: D/WebKit(29253): external/webkit/Source/WebCore/loader/appcache/ApplicationCacheStorage.cpp(558) : bool WebCore::ApplicationCacheStorage::executeSQLCommand(const WTF::String&)
01-10 16:11:34.466: E/SQLiteLog(29253): (1) no such table: Caches
01-10 16:11:34.466: D/WebKit(29253): ERROR:
01-10 16:11:34.466: D/WebKit(29253): Application Cache Storage: failed to execute statement "DELETE FROM Caches" error "no such table: Caches"
01-10 16:11:34.466: D/WebKit(29253): external/webkit/Source/WebCore/loader/appcache/ApplicationCacheStorage.cpp(558) : bool WebCore::ApplicationCacheStorage::executeSQLCommand(const WTF::String&)
01-10 16:11:34.466: E/SQLiteLog(29253): (1) no such table: Origins
01-10 16:11:34.466: D/WebKit(29253): ERROR:
01-10 16:11:34.466: D/WebKit(29253): Application Cache Storage: failed to execute statement "DELETE FROM Origins" error "no such table: Origins"
01-10 16:11:34.466: D/WebKit(29253): external/webkit/Source/WebCore/loader/appcache/ApplicationCacheStorage.cpp(558) : bool WebCore::ApplicationCacheStorage::executeSQLCommand(const WTF::String&)
01-10 16:11:34.476: E/SQLiteLog(29253): (1) no such table: DeletedCacheResources
关于如何解决这个问题有什么想法吗?
最佳答案
您的数据库是持久的,问题是每次您的应用程序启动时,在 onCreate() 中对 copy() 方法的调用会再次使用 Assets 中保存的副本覆盖数据库。我想你只想复制一次。一种方法是仅在目标目录不存在时才进行复制:
if (!CheckDirectory.exists()) {
CheckDirectory.mkdir();
...move the copy code inside this if block
}
或者您可以在共享首选项中存储一个标志,并在制作副本时将其设置为真。在 onCreate 中,仅当该标志为 false 时才执行副本。
编辑:
要使用共享首选项标志:
在onCreate中:
SharedPreferences sp = getSharedPreferences("MYPREFS", Activity.MODE_PRIVATE);
//If no shared prefs exist, e.g. first install, it doesn't matter - the following will return false as a default
Boolean database_copied = sp.getBoolean("database_copied", false);
if (!database_copied)
{
try {
String pName = this.getClass().getPackage().getName();
this.copy("Databases.db", "/data/data/" + pName + "/app_database/");
this.copy("0000000000000001.db", "/data/data/" + pName
+ "/app_database/file__0/");
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("database_copied", true);
editor.apply();
} catch (IOException e) {
e.printStackTrace();
}
}
关于android - 使用 Phonegap 从 SQLite 数据库为 Android 填充数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21040134/