Android读取大文件
我的 assets 文件夹中有一个 CSV 文件,其中包含 10000 多行数据。我想在创建数据库时将此数据插入到 SQLite 数据库中。我无法包含数据库,因为它是一个非常通用的应用程序,并且模型需要 CSV 文件。我不想一次性读取所有 10000 行数据并将其从内存中插入。如何有效、高效地完成任务?
I have a CSV file in the assets folder with more than 10000 lines of data. I want to insert this data into a SQLite database when the database is created. I cannot include a database because it is a very generic application and the model demands a CSV file instead. I don't want to read all 10000 lines of data and insert it from the memory in one stretch. How do I accomplish the task effectively and efficiently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
读完该行后,立即插入即可。因此,不要将这些行存储在内存中的某些数组列表中。
例如,
您可能希望在单个事务中执行此操作,以便在中途失败时可以回滚。在 JDBC 端,您可能需要考虑
PreparedStatement#addBatch()
/#executeBatch()
来批量执行插入。可以在此答案中找到示例。更新:作为一种完全不同但更有效的替代方案,您也可以将 Java 排除在故事之外并使用 SQLite 提供的 CSV 导入工具。
Just insert immediately once you've read the line. So, just don't store the lines in some arraylist in memory.
E.g.
You might want to do this in a single transaction so that you can rollback whenever it fails halfway. In the JDBC side, you may want to consider
PreparedStatement#addBatch()
/#executeBatch()
to execute the inserts in batches. An example can be found in this answer.Update: as a completely different but more efficient alternative, you can also leave Java outside the story and use CSV import facility provided by SQLite.
在这种情况下,最简单、最有效的方法是一次从文件中读取 20-30-50(或者可能是 100?!)行并插入到数据库中,但是您还需要运行一些测试来查看是什么您可以一次性阅读的最佳行数,然后执行:-)
the simplest and most efficient in this case is to read 20-30-50(or maybe 100?!) lines at a time from the file and insert into the database, however you also need to run a few tests to see what is the best number of lines that you can read in 1 go and then do it :-)
(来自 http://old.nabble.com/How-can-I-load-big-file-into-a-sqlite-database--td19202500.html - 就像在mysql中加载数据一样)。
其他:
禁用自动提交、自动刷新;读取 20 行,提交,刷新。
(from http://old.nabble.com/How-can-I-load-big-file-into-a-sqlite-database--td19202500.html - like LOAD DATA in mysql).
Other:
disable auto commit, auto flush; read 20 lines, commit, flush.