Android: how cancel/interrupt/break pending SQLite query?

半腔热情 提交于 2019-12-22 09:26:55

问题


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.


回答1:


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.




回答2:


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

ASyncTask.cancel(boolean mayInterruptIfRunning)



回答3:


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

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




回答4:


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



来源:https://stackoverflow.com/questions/3459493/android-how-cancel-interrupt-break-pending-sqlite-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!