7

I'm creating my first android app that will make use of SQlite. I have zero experience with databases, except for creating a mysql database to use with wordpress...


Edit: After doing some research about rest, I'm still confused about how rest, sqlite, and android dev fit together. My goal is to access a rest-based web service through a url and access certain datasets, then store them in my SQlite database. Then I want to access the contents of the database through my java program, and use them accordingly.

The datasets can be downloaded individually in CSV format, but because I will be using so many of them, I don't want to go through every line individually and store them in the database. I'm hoping there's a more efficient way to store these datasets in the database.

My main questions are:

  • How can I copy the XML contents of a webpage from a url into my sqlite database? Can I do this with my java program, through the sqlite database, or a java library?
  • Do I only need to copy the contents of the webpages from the url into the sqlite database one time? If so, what can I do if any information is changed in the datasets?
mdegges
  • 963
  • 3
  • 18
  • 39

3 Answers3

5

You first need a schema for your sqllite DB. That schema should map to the objects behind the web service. For e.g, you need a Person table in your DB if there is a Person entity on the web. It depends on what all you want to capture.

When you are done designing the schema, you should start writing the code that help you create & manage DB on android. This is done with the help of SQLiteOpenHelper class: http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

If you need to keep the DB synce'd with the data on the cloud (web services), you should implement sync. Android provides a very efficient sync framework.

Also, do watch this video from Android engineers explaining the best practices: http://www.youtube.com/watch?v=xHXn3Kg2IQE

Note, to actually fetch the data from the web service you would use UrlConnection API: http://developer.android.com/reference/java/net/URLConnection.html

This sample probably captures most of it. http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

Anirudh
  • 2,524
  • 1
  • 14
  • 14
3

In terms of reading CSV files, there are some good resources here:

Can you recommend a Java library for reading (and possibly writing) CSV files?

Once you have read each CSV line into an object, then you can turn around and persist it to the database. I'm the author of ORMLite so I'll talk about using it. I don't believe there is a hibernate port for Android.

There are a number of Android examples to help you to get up to speed with ORMLite. Also some good tutorials. If you want to write a number of rows at once then I'd recommend using the batch tasks ORMLite feature. For information, see the discussion about creating lists of objects on the mailing list.

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354
  • Thanks for your response. I didn't know this when I posted my question yesterday, but there is an API on the website that uses rest to provide access to the datasets. Because there are so many large datasets that I need to use, and now that I know there's a specific API, I updated my question accordingly. – mdegges Feb 20 '12 at 21:37
  • I would have created another question because now my answer now makes no sense. @mdegges – Gray Feb 21 '12 at 15:10
0

I can answer your first question about " I'm not sure how to add them efficiently"? yes, SQlite is very powerful and intelligent, you can add thousand of records in one transaction, just like traditional database, It significantly improve performance.

about second question, as my understanding, because CVS file is very simple, so you can download and analyze it by yourself.

Sean
  • 1,806
  • 1
  • 13
  • 15