简体   繁体   English

如何在Android的SQLite数据库中插入大量数据

[英]How to insert large amount of data in sqlite database in Android

I have a lot of data that is stored in a CSV file (about 20,100 rows), which I need to insert into a sqlite database. 我有很多数据存储在CSV文件中(约20,100行),需要将其插入sqlite数据库中。

This insert is taking very long to complete. 此插入内容需要很长时间才能完成。 What is the fastest way to insert this data? 插入此数据的最快方法是什么?

As you have suggested, number of rows are huge I will recommend not to use AsyncTask , as its not tied to your Activity lifecycle ie if you activity which started it dies, it doesnt mean AsyncTask dies as well, so if you try initiate a AsyncTask and somehow if your activity dies eg screen rotation or back key pressed, upon restarting another AsyncTask will get spawned rather then it getting linked to already executing AsyncTask . 正如您所建议的,行数很大,我建议您不要使用AsyncTask ,因为它与您的Activity生命周期没有关系,即,如果启动它的活动死亡,那并不意味着AsyncTask死亡,因此,如果尝试启动AsyncTask以及如果您的活动停止(例如,屏幕旋转或按下返回键),则在重新启动时将生成另一个AsyncTask而不是将其链接到已经执行的AsyncTask hence duplicating same operations. 因此重复相同的操作。

So, all in all I would recommend following approach 因此,总而言之,我建议您采用以下方法

(A) (一种)

  1. Create a IntentService , it's handleIntent() api already executes in a worker thread so you don't have to worry about any thing, and once all messaged in its queue are finished it automatically dies, so no worry at all about leaking any resources. 创建一个IntentService ,它的handleIntent() api已经在工作线程中执行,因此您不必担心任何事情,并且一旦队列中的所有消息完成,它就会自动死亡,因此完全不必担心泄漏任何资源。

  2. write your logic for inserting rows in bulk, use content resolver bulkInsert() api for same. 编写用于批量插入行的逻辑,使用内容解析器bulkInsert()api进行操作。 I will recommend inserting in 100 roes per batch, you can implement rollback and error checks to make sure insert goes normally. 我建议每批插入100个鱼卵,您可以执行回滚和错误检查以确保插入正常。

  3. Once all insert is finish, you can post back to your UI using Handler and Messengers. 完成所有插入操作后,您可以使用Handler和Messengers回发到您的UI。

with all this you will achieve two major challenge 通过所有这些,您将实现两个主要挑战

  1. Not to hang up your UI, escaping any possible ANR 不要挂断您的UI,避免任何可能的ANR
  2. Even if back key is pressed, ensured that db operation goes on smoothly as it was taken up in background task. 即使按下返回键,也要确保db操作顺利进行,因为它是在后台任务中处理的。

Using AsyncTask<> , insert 20,100 rows inserts in database. 使用AsyncTask<> ,在数据库中插入20,100行。 Using this asynctask whole work run in background. 使用此asynctask,整个工作在后台运行。 For more information follow this link 欲了解更多信息,请点击此链接

The best solution would be using services and executor because as OP described, process can take a lot time. 最好的解决方案是使用服务和执行程序,因为正如OP所述,过程可能会花费很多时间。 Thanks that You will be able to close app or move it to background with no worried Your long process is destroyed. 谢谢您将能够关闭应用程序或将其移至后台,而无需担心您的漫长过程被破坏了。

Using AsyncTask is not a good idea because it was designed for short operations as it is described on http://developer.android.com/reference/android/os/AsyncTask.html You must also be careful with using it. 使用AsyncTask并不是一个好主意,因为它已针对http://developer.android.com/reference/android/os/AsyncTask.html中所述的简短操作进行了设计。您还必须小心使用它。 Changing orientation screen cause recreating view and also task of asynctask. 更改方向屏幕会导致重新创建视图以及asynctask的任务。

AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask. 理想情况下,应将AsyncTasks用于较短的操作(最多几秒钟)。如果需要使线程长时间运行,则强烈建议您使用java.util.concurrent包提供的各种API,例如执行程序,ThreadPoolExecutor和FutureTask。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM