3

In my Android application user can fill database with imported data, then perform predefined, complex SQL query. With some data pattern, query will take very long (minutes and more on HTC Hero), while common usage will not exceed 10-15 seconds.

Is there any way to cancel pending SQLite query on Android? Timeout would be as good as well, partial results are not required. I'm performing query within AsyncTask.doInBackground()

I know best way is optimize query or change app logic, but in this case results depends on user knowledge anyway, so I'd like to help those who enter troublesome pattern.

tomash
  • 12,742
  • 15
  • 64
  • 81

4 Answers4

3

You can't cancel pending sql query. It's done in SQLite engine. SQLite has native methods to interrupt query, but it's not accessible in java interface.

The best would be to improve query performance.
Read this: http://www.sqlite.org/cvstrac/wiki?p=PerformanceTuning

You can divide long running queries to smaller by limiting row number. Then it would be easier to cancel query that is performed in chunks.

pawelzieba
  • 16,082
  • 3
  • 46
  • 72
0

ASyncTask supports cancellation and the flag should cancel all, including your SQL Query.

ASyncTask.cancel(boolean mayInterruptIfRunning)
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • 5
    This is not enough. Code from `doInBackground()` is in fact left alone as AsyncTask flow returns via `onCancelled()`, but Cursor still hangs on `moveToFirst()` and sucks whole CPU. I tried to close SQLiteDatabase or SQLiteOpenHelper but any call to this objects blocks execution until `moveToFirst()` is done. And Cursor has no "cancel" method. – tomash Aug 11 '10 at 19:36
0

You can cancel running queries on sqlite by using android.os.CancellationSignal which can be passed to some of the methods of android.database.sqlite.SQLiteDatabase

dynamo
  • 1,123
  • 10
  • 14
0

SQLite provides a intrerupt method: http://www.sqlite.org/c3ref/interrupt.html

I'm not sure how you would call that in Android though.

Cristi
  • 1,488
  • 1
  • 14
  • 14