使用 java (android) 连接数据库的多个文件会导致数据库损坏

发布于 2024-11-18 18:20:02 字数 1002 浏览 0 评论 0原文

我正在尝试在 android 中加入数据库的多个文件。该文件首先被分割,因为 2.3 之前的 Android 版本的 apk 中任何文件的大小限制为 1MB。我试图连接在一起的文件是一个数据库,它以自己的方式很复杂,要放置在正确的文件夹中以便 SQLite 读取它,我使用以下方法:

private void copyDB() throws IOException {
        // list the files the db is splitted into
        int[] cc = { R.raw.cc1, R.raw.cc2, R.raw.cc3 };
        OutputStream out = new FileOutputStream(DB_PATH+DB_NAME);

        for (int k : cc) {
            InputStream in = mContext.getResources().openRawResource(k);
            int count;
            byte[] filebytes = new byte[1024];
            while((count = in.read(filebytes)) != -1) 
                out.write(filebytes, 0, count);
            in.close();
        }

        // clean up
        out.flush();
        out.close();
    }

如您所见,我使用普通流将文件重新连接在一起,我几乎肯定它可以工作,因为它在 android 2.3 中的性能符合预期。但是,当我在 android 2.2 上测试时,我得到了一个损坏的数据库(我没有来自 LogCat 的确切消息,但 SQL 错误是“错误:数据库磁盘映像格式错误”)

有什么我可以做的吗当我将数据库放在 /data/data/package.name/database/ 文件夹中时,如何防止数据库损坏? 感谢您抽出时间。

I'm trying to join several files of a database in android. The file was split in the first place because of the limitation in 1MB of size of any files within the apk for android versions prior to 2.3. The file I'm trying to join together is a database, which is complicated in its own way to be placed in the right folder for SQLite to read it, and I'm using the following method:

private void copyDB() throws IOException {
        // list the files the db is splitted into
        int[] cc = { R.raw.cc1, R.raw.cc2, R.raw.cc3 };
        OutputStream out = new FileOutputStream(DB_PATH+DB_NAME);

        for (int k : cc) {
            InputStream in = mContext.getResources().openRawResource(k);
            int count;
            byte[] filebytes = new byte[1024];
            while((count = in.read(filebytes)) != -1) 
                out.write(filebytes, 0, count);
            in.close();
        }

        // clean up
        out.flush();
        out.close();
    }

As you can see, I'm using plain streams to join the files back together, and I'm almost positive that it works because it performs as expected with android 2.3. However, when I test on android 2.2 I'm getting a corrupt database (I don't have with me the exact message from the LogCat but the SQL error was 'Error: database disk image is malformed')

Is there anything that I can do to prevent the database from corrupting when I place it in the /data/data/package.name/database/ folder?
Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满地尘埃落定 2024-11-25 18:20:02

严格来说,这并不是您问题的答案,但可能会解决您的问题。当我遇到这个问题时,我相信我重命名了该文件以强制 Android 将其视为不可压缩的二进制文件(为其提供了 .mp3 扩展名)。

This is not strictly the answer to your question, but may resolve your problem. When I encountered this problem, I believe I renamed the file to force Android to treat it as an uncompressable binary (gave it an extension of .mp3).

就像说晚安 2024-11-25 18:20:02

我遇到了同样的问题,但是我意识到这是我错误地分割数据库的方式。本来我是这样划分的;

split largeFile.db -b 1M largeFile

我忘记了第二个largeFile末尾的.db,因此,

split largeFile.db -b 1M largeFile.db

解决了我的问题。然后我将所有的largeFile.dbaa,largeFile.dbab,...移动到资产(我想原始的你的情况也可以)文件夹中并将它们重命名为largeFileA,largeFileB,...

也许这有帮助

I had the same problem, however I realized that it was the way I was splitting my db incorrectly. Originally I split it this way;

split largeFile.db -b 1M largeFile

i was forgetting the .db at the end of the second largeFile hence,

split largeFile.db -b 1M largeFile.db

solved my problem. I then moved all my largeFile.dbaa, largeFile.dbab, ... in the assets (I suppose raw your case works as well) folder and renamed them to largeFileA, largeFileB,...

Maybe this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文