Hello Guys!!! Hopes things are well at your end.
Today I am going to discuss Data storage Option in android. Particularly about sqlite in android. How to use its efficiently and effectively in your application without any error.
There are five ways to store data in Android:
Today I am going to discuss Data storage Option in android. Particularly about sqlite in android. How to use its efficiently and effectively in your application without any error.
There are five ways to store data in Android:
- shared preferences
- internal files
- external files
- SQLite and
- network storage in the cloud
Let’s review each.
1. Shared preferences are internal to the application and device. This data is not available to other applications. The user cannot directly manipulate this data by mounting onto a USB port. The data is
removed automatically when the application is removed. Shared preferences are structured key/value
pair data and follow a few other semantics imposed by Android for using the stored data as preferences. Shared preferences are maintained as XML files.
2. Internal files are unstructured private files that an application can create and store data in. Like shared preferences, internal files follow the life cycle of the application. When the application is uninstalled, the internal files created by the application are also removed.
3. External files are stored on the SD card. These become public files that other apps including the user could see outside the context of your application. External files are typically used for data that is primarily created by the user and is meaningful outside of the app that creates it—for example, audio files, video files, word documents, etc. External files are also suitable when the amount of data is large, say 10MB or more.
4. SQLite is a relational database. For structured data, like the data that you are planning to use JSON for, SQLite is preferred. However, it is lot of work to go between Java objects and a relational database. Even in the simplest case of using wonderfully crafted o/r mapping tools or libraries, it is still lot of work. However, SQLite is an excellent option for tuning and refactoring your application for subsequent releases. This refactoring will make your application respond much faster and use much less power. The database also is private to the application and not available to outside apps unless you wrap the database in a content-provider framework.
5. Network storage allows your application to choose to save persistent data in the cloud. Network
storage cannot be a complete option for lot of apps, however, as you likely will need the app to work when disconnected from the Internet. There may be supplemental opportunities to use parse.com or a similar BaaS (backend as a service) platform to do a hybrid approach, whereby some data is stored in the cloud and is synced when needed with the device.
Now after reviewing each you can understand which one is better suited your need. I am going to put some more light on sqlite database use in Android Application.
SQLite is at the heart of Android’s database support. This database was developed with embedded environments in mind – and is used not only by Android but also by Apple’s iOS and Blackberry’s system as well as lots of other systems with low memory footprint and comparatively little CPU horsepower.
SQLite is so dominant in the embedded and also the mobile world. The main reasons are
- Low memory consumption
- Ease of use
- Free availability
- SQLite is serverless
- SQLite stores data in one database file
- SQLite offers only a few data types
- SQLite uses manifest typing instead of static types
- SQLite has no fixed column length
- SQLite uses cross-platform database files
SQLite datatypes
Type | Meaning |
---|---|
NULL | The null value |
INTEGER | Any number which is no floating point number |
REAL | Floating-point numbers (8-Byte IEEE 754 – i.e. double precision) |
TEXT | Any String and also single characters (UTF-8, UTF-16BE or UTF-16LE) |
BLOB | A binary blob of data |
SQLite is written in the C programming language while Android applications are primarily developed using Java. To bridge this “language gap”, the Android SDK includes a set of classes that provide a Java layer on top of the SQLite database management system. Here I am providing a basic overview of each of the major classes within this category. More details on each class, You can find in the online Android documentation.
Cursor
A class provided specifically to provide access to the results of a database query. For example, a SQL SELECT operation performed on a database will potentially return multiple matching rows from the database. A Cursor instance can be used to step through these results, which may then be accessed from within the application code using a variety of methods. Some key methods of this class are as follows:
- close() – Releases all resources used by the cursor and closes it.
- getCount() – Returns the number of rows contained within the result set.
- moveToFirst() – Moves to the first row within the result set.
- moveToLast() – Moves to the last row in the result set.
- moveToNext() – Moves to the next row in the result set.
- move() – Moves by a specified offset from the current position in the result set.
- get<type>() – Returns the value of the specified <type> contained at the specified column index of the row at the current cursor position (variations consist of getString(), getInt(), getShort(), getFloat() and getDouble()).
SQLiteDatabase
This class provides the primary interface between the application code and underlying SQLite databases including the ability to create, delete and perform SQL based operations on databases. Some key methods of this class are as follows:
- insert() – Inserts a new row into a database table.
- delete() – Deletes rows from a database table.
- query() – Performs a specified database query and returns matching results via a Cursor object.
- execSQL() – Executes a single SQL statement that does not return result data.
- rawQuery() – Executes an SQL query statement and returns matching results in the form of a Cursor object.
SQLiteOpenHelper
A helper class designed to make it easier to create and update databases. This class must be subclassed within the code of the application seeking database access and the following callback methods implemented within that subclass:
- onCreate() – Called when the database is created for the first time. This method is passed as an argument the SQLiteDatabase object for the newly created database. This is the ideal location to initialize the database in terms of creating a table and inserting any initial data rows.
- onUpgrade() – Called in the event that the application code contains a more recent database version number reference. This is typically used when an application is updated on the device and requires that the database schema also be updated to handle storage of additional data.
In addition to the above mandatory callback methods, the onOpen() method, called when the database is opened, may also be implemented within the subclass.
The constructor for the subclass must also be implemented to call the super class, passing through the application context, the name of the database and the database version. Notable methods of the SQLiteOpenHelper class include:
- getWritableDatabase() – Opens or creates a database for reading and writing. Returns a reference to the database in the form of a SQLiteDatabase object.
- getReadableDatabase() – Creates or opens a database for reading only. Returns a reference to the database in the form of a SQLiteDatabase object.
- close() – Closes the database.