1

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.

This insert is taking very long to complete. What is the fastest way to insert this data?

3
  • 1
    Using AsyncTask<> Insert 20,100 rows inserts in database. Using this asynctask hole work run in background. For more information follow this link developer.android.com/reference/android/os/AsyncTask.html Commented Jul 28, 2015 at 6:07
  • Its takes too much time. I tried also. @JigarShekh Commented Jul 28, 2015 at 6:56
  • You have to qualify what "long time" means. Provided you coded is right, it shouldn't take more than a few seconds. ContentResolver.bulkInsert should make it even faster. Yet, I suggest you rethink your application design: do you really need to insert so much data during runtime? You may consider creating and populating your database when developing the app and including it with your application instead. Commented Jul 28, 2015 at 6:57

3 Answers 3

2

As you have suggested, number of rows are huge I will recommend not to use AsyncTask, as its not tied to your Activity lifecycle i.e 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 e.g screen rotation or back key pressed, upon restarting another AsyncTask will get spawned rather then it getting linked to already executing 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.

  2. write your logic for inserting rows in bulk, use content resolver bulkInsert() api for same. I will recommend inserting in 100 roes per batch, you can implement rollback and error checks to make sure insert goes normally.

  3. Once all insert is finish, you can post back to your UI using Handler and Messengers.

with all this you will achieve two major challenge

  1. Not to hang up your UI, escaping any possible ANR
  2. Even if back key is pressed, ensured that db operation goes on smoothly as it was taken up in background task.
Sign up to request clarification or add additional context in comments.

3 Comments

I am newbie in android development so Can you explain me how to use of it. @Techfist
start coding, and share your effort will help you along. cant write entire logic for you, try doing what I have already written, if you face trouble write me back.
Okay, I will try if any issue I will ask you again @Techfist
0

Using AsyncTask<>, insert 20,100 rows inserts in database. Using this asynctask whole work run in background. For more information follow this link

1 Comment

Asynctask is not safe if you don't control the orientation. The best way would be using service so process will be able to finish even when app is in background or closed. AsyncTask was not designed for very long processes.
0

The best solution would be using services and executor because as OP described, process can take a lot time. 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. Changing orientation screen cause recreating view and also task of 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.

2 Comments

Okay but I am newbie in android development so how to use of it? Can you help me? @deadfish
There are plenty books and online tutorials describing how service works. Just look around.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.