31

Why do we use the sqlite data base in android.I am developing an android application where the data is to be fetched from the server and do some data calculation and show it on the UI.

Is it good for me to fetch the data into the sqlite DB and update the UI on regular interval from the sqlite in evry 20 minutes or will it be good to sent the Http get request to the server and update the data from teh response on the UI.

I wanted to know which one will be better and why?Why to involve sqlite DB? The data corresponds to some 40X40 table data on which some heavy mathematical processing is to be done and then displayed on the UI(similar to Stock market application) and data needs to be cleared after every 12 hours. plz advice Rgds, Raul

Raulp
  • 7,758
  • 20
  • 93
  • 155

5 Answers5

30

It is good to use database in your case.

Pros:

  • If your application gets closed the in memory data will be lost, but after that you will be able to restore the state from the database if you have one
  • Especially for the case of complex calculations it is good to store the result once in the database and not recalculate it multiple times on demand
  • The database will untie your UI from the internet connection and thus you will be able to display results even if there is not internet connection
  • Using database you will be able to fetch the updated data from a background service, without impacting your UI
  • Organizing your data in database usually makes it a lot easier to manage all the application data.

Cons:

  • Adding database will require a bit of additional effort on your side

As you can see my list proves you SHOULD use database in your case. Maybe I am biased, but at least I provide you with things to consider.

Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
5

It's really a design decision, SQLite offers a very robust way to organize and persist your data, you're only other options are to write to a file, or to save in SharedPrefs, both methods become a lot harder to manage once the size of your data begins to grow, as you must manually keep a list of objects and manage their names etc etc. 40 x 40 table data is large enough to justify using SQLite, even if you are dropping and recreating the table every 12 hours.

You might want to consider using an ORM library to make fetching and saving data from the DB simpler, ORMLite is good and compatible with Android

http://ormlite.com/

Sam Clewlow
  • 4,293
  • 26
  • 36
  • can I use it Android?how good it will be from the sqlite?Why prefer this than Sqlite?Sqlite is inbuilt , this is external( i may have to use jar of it) – Raulp Apr 26 '12 at 08:52
  • 2
    @softy Ormlite is just auxiliary library that should make it easier to use the SQLite (making object relational mapping). Basically that way you will not need to worry about writing/ reading procedures. I have used it in Android. Furthermore the performance impact of adding additional library is insignificant. – Boris Strandjev Apr 26 '12 at 08:57
4

If your application relies heavily on an internet connection you don't need to buffer information in the database. However if you want to use the app where you have bad or no signal you might want to used cached values from the sqlite database. With slow internet connection your application may be unresponsive so caching may be a good idea - but it doesn't necessarily be in the sqlite database. You should use the sqlite database for data that is required by the device frequently and that is irrelevant to your server component. If the data is updated frequently but only while the application runs you might want to cache in the devices memory. I assume your app is not running all the time within the 12 hours but is called regularly instead to check something.

konqi
  • 5,137
  • 3
  • 34
  • 52
0

12hrs is a long time, so rather than leaving your data wander in RAM, i would suggest you to use database. Because you never know when you may need to read it again.

Otherwise, if your purpose is only to downloaded data, process it and display in activity, then you dont need to involve database because if your app is closed (due to user or low memory), in anyway your app will be downloading fresh data from server... am i right?

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • yes data will be added (automatically the request will be sent to the server after every 30 minutes(configurable) and when the user wants to see the data on the UI he has to manually press/touch some UI button which will displays the data (after calculation), if he presses before teh configurable time the data would be same and in case if he presses it > 30 minutes , he will getthe most recent data. – Raulp Apr 26 '12 at 08:54
  • then i dont think you need to bring the hassle of database in your requirements. Its pretty simple and straight forward thing to do without database – waqaslam Apr 26 '12 at 08:57
0
> update the UI on regular interval from the sqlite in evry 20 minutes 

Dont expect your app to be open for such a long duration.

To precisely suggest to your case

 Avoid DB 

 Fetch Data at app start or at appropriate time when app is opened 
 and save it in plain java objects.

 Define Methods within it that perform operation in it.

 Make map or list to save those POJO

 Define Seprate Controller Classes within your project to update map of pojo at any     
 specific change to make fresh data available to UI.
Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72
  • Fetch Data at app start or at appropriate time when app is opened and save it in plain java objects. >>>>>>>>> Will creating /organizing data for a 40X40 data be good in plain java objects? , yes it can be mapped via list or dictinary. – Raulp Apr 26 '12 at 08:57
  • Make subclasses and use DataStructure Within. Everything can be Framed via OOPS even it is 80X80 – Rohit Sharma Apr 26 '12 at 09:04