Tuesday, 12 March 2013

What is SQLite & its Architecture?

SQLite is an open Source Database which is embedded into android. SQLite support standard relational database feature like SQL syntax, transactions and prepared statements. It requires very  little memory at runtime(250 KByte).

SQLite Architecture

the package android.database contain all general classes for working with databases android.database.sqlite contain the SQLite specific classes.

SQLite Open Helper :- To create and upgrade a database in your Android application you usually subclass SQLite OpenHelper. In the constructor of your subclass you call the super() method of SQLIteOpenHelper, specifying the database name and the current address version.

onCreate() is called by the framework, if the database does not exits.

onUpgrade() is called , if the database version is increased in your application code, This method allow you to upgrade the database schema.

Both methods receive an SQLite Database object as parameter which represents the database.

SQLiteOpenHelper provides the methods getReadableDatabase(0 and getWriteableDatabase(0 to get access to an SQLiteDatabase object either in read or write mode.

The Database tables should use the Identifier _id for the primary key of the table. It is the best practice to create a separate class per table.The class define static onCreate(0 and onUpgrade() method.

SQLite Database :- is a base class for working with a SQLite database in Android and provides method to open, query , update and close the database.

SQLiteDatabase provides the insert(), update() and delete() methods. It provide the exeeSQL() method , which allows to execute SQL statement directly.

The object contain values allows to define Key/values . The "Key" represent the table column identifier and the "value" represents the content for the table record in this column. Content values are used for inserts and updates of database queries.

Queries can be created via the rawQuery() and query() methods via the SQLiteQuerybuilder class.

rawQuery() directly accepts an SQLite select statement as input.

query() provides a structures interface for specifying the sql query,

SQLiteQueryBuilder is a convenience  class that helps to build SQL queries.

Paramaters od the query() method.

String dbName :- The table name to compile the query object against.

String [ ] columnName :- A list of which table columns to return. Passing "null" will return all accounts.

String whereClause :- where clause , i.e. filter for the selection of data , null will select all data.

String [ ] selectionArgs :- you may include ?s in the "where Clause". These placeholder wil get replaced by the values from the selectionArgs array.

String [ ] groupBy :- A filter declaring how to group rows , null will cause the rows to not be grouped.

String [ ] having :- Filters for the groups , null means no ordering.

String [ ] orderBy :- table columns which will be used to order the data, null means no ordering.

if a condition is not required you can pass null. For. Ex for the groupBy clause

No comments:

Post a Comment