从在线数据库同步后,从Android应用程序获取sqlite数据库

时间:2017-02-02 03:48:52

标签: android android-studio sqlite

我有一个带有prefiiled数据库的Android应用程序,我可以破解预先存储的数据,但在与在线服务器同步后,我仍然得到旧的数据库。我认为新数据存储在内存中,而不是写入存储在assests中的数据库。 如何才能获得实时sqlite数据库,app目前正在同步后使用。

2 个答案:

答案 0 :(得分:0)

比较旧数据库和新数据库中可能更改的所有对象。删除不存在的记录,并插入新记录。

或者只是删除旧数据库,并下载新数据库作为替换。

答案 1 :(得分:0)

通过拉取该文件,您无法获取新数据。 您可以尝试这一点,它将读取您的数据并从中创建新文件。

public boolean backUpDB() {
        String currentDBPath = ""; //db path
                + "DB_NAME";
        File data = Environment.getDataDirectory();

        if (isExternalStorageAvailable()) {
            File folder;
            folder = new File(
                    context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
                            + "/backup_path/");
            if (!folder.exists()) {
                folder.mkdir();
            }
            String path = folder + "";

            File SDPath = new File(path);
            if (!SDPath.exists()) {
                SDPath.mkdir();
            }
            try {
                File currentDB = new File(data, currentDBPath);
                InputStream input = new FileInputStream(currentDB);
                byte dataa[] = new byte[input.available()];
                input.read(dataa);

                OutputStream out = new FileOutputStream(path + "/"
                        + "DB_NAME");
                out.write(dataa);
                out.flush();
                out.close();
                input.close();
            } catch (Exception e) {
                Commons.printException("exception," + e + "");
                return false;
            }
            return true;
        } else {
            return false;
        }

    }