Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Rest to store data in Sqlite

Tags:

android

sqlite

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?
like image 288
mdegges Avatar asked Feb 18 '12 07:02

mdegges


People also ask

Is SQLite a REST API?

Why is SQLite REST API Useful? Copies of the SQLite platform are found on every iPhone and Android device, Windows and Mac computers, the Google Chrome and Firefox web browsers, and countless other use cases. Building a SQLite REST API is crucial in order to gain access to the data found in these ubiquitous databases.

Can we store files in SQLite?

From the standpoint of SQLite, an image is no different than any other type of file. You are welcome to store file contents in a BLOB column.

Why SQLite database is a better option of storing the data?

sqlite packages offer a higher-performance alternative where source compatibility is not an issue. There is no file parsing and generating code to write and debug. Content can be accessed and updated using powerful SQL queries, greatly reducing the complexity of the application code.


2 Answers

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

like image 130
Anirudh Avatar answered Nov 14 '22 23:11

Anirudh


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.

like image 21
Gray Avatar answered Nov 14 '22 21:11

Gray