android - 使用 Phonegap 从 SQLite 数据库为 Android 填充数据

标签 android sqlite cordova

我正在为基于 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/

相关文章:

android - 想要在整数列表中返回 0 IF SUM(something) 为 null

android - 如何在 Cordova 应用程序上检查互联网连接?

java - 如何使用依赖Java项目的Ant构建Android项目?

Android 将现有数据库导入项目时出错

Android ListView 项目在滚动时重叠

python - 为什么 SQLite Blob 类型在 pandas Python 中自动转换为 unicode 序列

android sqlite 查询慢

java - 方法 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)

ios - Cordova + Bootstrap : correct way to start a website a little lower

plugins - Cordova.exec 函数不运行 native 函数