【发布时间】:2014-01-10 09:11:04
【问题描述】:
我将 SQLLite 用于我的基于 phonegap 的 Android 应用程序。我面临的问题是,只要应用程序打开,我就能够获取我存储在 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
知道如何解决这个问题吗?
【问题讨论】: