Saturday, September 29, 2012

Android - SQLite Database

Android provide SQLite database for an storage option. This post will provide a overview on how to use it.

SQLite Database

It is a private storage space for application. It is persistent, suitable for complex data structure and long term storage. The database will be removed when you uninstall the application.


SQLiteOpenHelper Overview

SQLiteOpenHelper is a helper class that manage SQLite database and version management. In order to use SQLite database, you need to extend this class for database management.

When extending this class, you will need to create a constructor and override 2 methods

Constructor - in the construct, you will need to call the super() method. This method is the primary method that allow you to define the database and database version

onCreate() - This method will be called if the database does not exist. It is a place where you can create your database schema

onUpgrade() - This method will be called if the database version is increased in the code. It is a place where you want to modify your database schema.

SQLiteDatabase Overview

SQLiteDatabase is a class that provide methods to perform SQL query

insert() - insert data into the table with the help of ContentValues class

query() - query the table and get the values. This method return a database cursor that allow user to iterate over the items

delete() - delete data from a table

execSQL() - execute an SQL statement such as CREATE that is not SELECT and does not return data

Creating the Database and Table

If the database does not exist, the framework will call onCreate(SQLiteDatabase database) method. You must use the method to create the new database and table. Inside onCreate, use execSQL() to create the table

Overall process is as follow

  • Override the constructor and call super() that provide your database name and version number
  • Override onCreate(SQLiteDatabase database), inside the method, call SQLiteDatabase.execSQL() to call CREATE SQL statement to create your database


Inserting Data into Table

Overall process is as follow
  • Open and obtain database via SQLiteOpenHelper.getWritableDatabase()
  • Put column name and value pair into ContentValues class
  • Call SQLiteDatabase.insert() with ContentValue to add row into table
  • Call SQLiteOpenHelper.close() to close the database


Query Data from Table

Overall process is as follow
  • Open and obtain database via SQLiteOpenHelper.getWritableDatabase()
  • Call SQLiteDatabase.query() to obtain a database Cursor
  • Call Cursor.moveToFirst() to reset the cursor to first item
  • Iterate the items with the help of Cursor.isAfterLast()
  • Get the item value via Cursor.getXXX(index) where XXX can be String, Int, Long, etc.. and index is the table column index
  • Move to next item via Cursor.moveToNext()
  • After accessing all items, close the Cursor with Cursor.close
Sample Code



Sunday, September 23, 2012

Android - Storage Options

Android provides many ways for developers to store their application data. Developers should understand their application requirements before selecting the method to store their data.

This post is to provide a high level view on how to select the application storage options

Shared Preference

Shared Preference allow you to store primitive data type as key-value pair. The data is persistent across user session even if your application is killed. Shared Preference data can only be access within the same application. Also, Shared Preference stored the data into memory after first access from the application. So, it is not recommendable for large dataset.

Shared Preference is suitable for


  • Small and simple storage. Just a key and a small dataset with simple data structure
  • Data that requires frequent access
  • Passing data between Activities

Internal Storage

Internal Storage save your data into application private folder. These saved data cannot be access by other application. When you uninstall your application, your application data in internal storage will be deleted.

Internal Storage is suitable for


  • Large dataset with complex data structure and does not require to shared with other application
  • Data that are not frequently used but require permanent storage

Internal Storage with Cache Directory

Internal Storage allow you to cache your file with the use of getCacheDir(). Android may delete these cache files if the system is low in Internal Storage.


Internal Storage with Cache Directory is suitable for


  • Large dataset that is only required temporarily and the lost of the data will not impact the application

External Storage

External Storage allow you stored large dataset and shared between different applications. The external storage can be SDCard or Internal Storage.

External Storage is suitable for

  • Large dataset that you wish to shared between different application

External Storage with Cache Directory

External Storage allow you to cache your file with the use of getExternalCacheDir(). Android may delete these cache files if the system is low in External Storage


External Storage with Cache Directory is suitable for


  • Large dataset that is only required temporarily shared between application and the lost of the data will not impact the application


Database

Android support SQLites database. Developers can create complex database schema for their application to store their data. Database can only be access by name with any class in the application

Database is suitable for


  • Large complex and logical dataset that require database schema to ensure efficient data access


Reference: http://developer.android.com/guide/topics/data/data-storage.html

Facebook - Control privacy setting of liked page

By default, Facebook displayed your liked page to public. It is the user responsibility to adjust the privacy setting. And in my opinion, F...