I have a requirement in my application wherein I have to every now and then perform insert and update queries on the database. Now, my question is shall maintain cache within the application and after specific intervals or may be after say 20 entries, commit the cache into the database?
Insert operation is fairly fast in SQLite. Make sure you are closing the database in finally block to avoid db in opened state. That can block further inserts. As much as possible, use batch inserts, they are much faster. See here
If this is the scenario where shall I maintain the cache. Should it be on the application level or in the service?
Service also comes at the application level :) . There is a possibility of maintaining the cache in a service and making the service live in a separate process. See here.
There is a possibility that say the application abruptly crashes and hence the data that is there in the cache no longer persists.
The only option that stays is, either maintain the cache in other process(which can again crash anytime) or maintain it on disk, may be internal storage. Maintaining a cache on internal storage is as good or bad as writing to sqlite.
What can be the possible scenarios for this.
You implement cache or you dont.
I have read somewhere that everytime opening and closing the database is an overhead. Note: my database resides in the sdcard.
AFAIK, Opening an SQLite is comparable to opening a file(a little slower though) and not comparable to opening an Oracle database. So most of the times it is affordable to open the database, but batching inserts to improve performance will never hurt ;)