如何读取大型 Sqlite 文件并从资产文件夹复制到 Android 模拟器或设备中?

发布于 2024-09-03 07:11:28 字数 1750 浏览 4 评论 0原文

我想很多人已经读过这篇文章:

在 Android 应用程序中使用您自己的 SQLite 数据库:http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page -2/#comment-12368

时,它不断带来 IOException

while ((length = myInput.read(buffer))>0){
    myOutput.write(buffer, 0, length);
}

但是,在我尝试使用大型数据库文件 。 大小>8MB 我在 Mac OS X 中使用 sqlite3 构建它,插入 UTF-8 编码字符串(因为我使用韩语), 添加了 android_meta 表,其中 ko_KR 作为区域设置,如上所述。

但是,当我调试时,它不断显示 IOException,

length=myInput.read(buffer)

我怀疑这是由于尝试读取大文件引起的。如果没有,我不知道为什么。 我使用更小的文本文件测试了相同的代码,并且运行良好。

有人可以帮我解决这个问题吗?我搜索了很多地方,但没有一个地方给我明确的答案或好的解决方案。 好的意思是高效或简单。

我会尝试使用BufferedInput(Output)Stream,但如果更简单的不能工作,我认为这也不起作用。

谁能解释一下 Android 中文件输入/输出的基本限制,以及可能的解决方法吗? 我真的很感激任何人的体贴回答。谢谢。

更多细节:

private void copyDataBase() throws IOException{

     //Open your local db as the input stream
     InputStream myInput = myContext.getAssets().open(DB_NAME);

     // Path to the just created empty db
     String outFileName = DB_PATH + DB_NAME;

     //Open the empty db as the output stream
     OutputStream myOutput = new FileOutputStream(outFileName);

     //transfer bytes from the inputfile to the outputfile
     byte[] buffer = new byte[1024];
     int length;
     while ((length = myInput.read(buffer))>0){
      myOutput.write(buffer, 0, length);
     }

     //Close the streams
     myOutput.flush();
     myOutput.close();
     myInput.close();

    }

I guess many people already read this article:

Using your own SQLite database in Android applications: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-2/#comment-12368

However it's keep bringing IOException at

while ((length = myInput.read(buffer))>0){
    myOutput.write(buffer, 0, length);
}

I’am trying to use a large DB file. It’s as big as >8MB
I built it using sqlite3 in Mac OS X, inserted UTF-8 encoded strings (for I am using Korean),
added android_meta table with ko_KR as locale, as instructed above.

However, When I debug, it keeps showing IOException at

length=myInput.read(buffer)

I suspect it’s caused by trying to read a big file. If not, I have no clue why.
I tested the same code using much smaller text file, and it worked fine.

Can anyone help me out on this? I’ve searched many places, but no place gave me the clear answer, or good solution.
Good meaning efficient or easy.

I will try use BufferedInput(Output)Stream, but if the simpler one cannot work, I don’t think this will work either.

Can anyone explain the fundamental limits in file input/output in Android, and the right way around it, possibly?
I will really appreciate anyone’s considerate answer. Thank you.

WITH MORE DETAIL:

private void copyDataBase() throws IOException{

     //Open your local db as the input stream
     InputStream myInput = myContext.getAssets().open(DB_NAME);

     // Path to the just created empty db
     String outFileName = DB_PATH + DB_NAME;

     //Open the empty db as the output stream
     OutputStream myOutput = new FileOutputStream(outFileName);

     //transfer bytes from the inputfile to the outputfile
     byte[] buffer = new byte[1024];
     int length;
     while ((length = myInput.read(buffer))>0){
      myOutput.write(buffer, 0, length);
     }

     //Close the streams
     myOutput.flush();
     myOutput.close();
     myInput.close();

    }

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

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

发布评论

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

评论(3

指尖凝香 2024-09-10 07:11:28

谁能帮我解决这个问题吗?

使用较小的文件。或者在解包时将一组较小的文件拼接成一个大文件。或者在第一次运行应用程序时下载数据库。

Can anyone help me out on this?

Use a smaller file. Or a set of smaller files that you stitch together into a large file as you are unpacking them. Or download the database on the first run of your application.

浅听莫相离 2024-09-10 07:11:28

我去过那里。我相信 RAW 资源类型没有最大大小限制(而资产,或更具体地说,连接到资产的流的大小受到限制)。

一旦你克服了这个障碍,你应该就没问题了 - 我手机上的数据库已经超过 100MB。

I've been there. I believe the RAW resource type is free of maximum size constraints (whereas Assets, or more specifically, the stream connected to an asset, is limited in size).

Once you get past that hurdle you should be fine - I've had databases on my phone well over 100MB.

浅紫色的梦幻 2024-09-10 07:11:28

不知道它是否对您有帮助,但如果其他方法失败,您可以尝试使用 NIO。例如,这是一个实现 将输入流复制到文件输出。或者,您可以尝试压缩文件并查看 Android 解压缩是否成功工具可以直接解压到你的db文件夹中。

Don't know if it would help you, but if other approaches fail you could try using NIO. For example, here's an implementation of copying inputstream to fileoutput. Alternatively, you could try zipping your file and see if the Android unzipping tools can unzip directly to your db folder.

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