Sqlite Database in Android

AndroidHeight
3 min readJun 13, 2017

--

What is SQLite database?

SQLite is not a client-server database engine.It is embedded into the end program.SQLite is
an in-process library that implements a self-contained, server-less, zero-configuration, transactional

SQL database engine. SQLite a popular database engine choice on memory constrained gadgets such as cellphones, PDAs, and MP3 players. SQLite requires of minimum space (4KiB).

SQLite in Android:

  • SQLite is embedded into every Android device. Using an SQLite database in Android does not require a setup procedure or administration of the database.
  • You have to take care of SQL query for creating and updating database.After that android platform automatically create the database and table.
  • Access to an SQLite database involves accessing the file system. This can be slow. Therefore it is recommended to perform database operations asynchronously.
  • If your application creates a database, this database is by default saved in the directory DATA/data/APP_NAME/databases/FILENAME.

SQLite architecture:

SQLite Package — In Android, android database contains all the necessary class related to database. The android.database.sqlite contains sqlite specific class.

To create sqlite database in android, You have to extends SQLiteOpenHelper class.

You have to override two methods :-

1) onCreate() : This method called when the database is created first time. This method is use for crating tables .

2) onUpgrade() : It is called when the the database needs to be upgraded. you should use this method to drop tables,add table or do anything else it needs to upgrade to the new schema version.

If you add new columns you can use ALTER TABLE to insert them into a live table.If you rename or remove columns you can use ALTER TABLE to rename the old table,then create the new table and then populate the new table with the contents of the old table.

Both methods receive an SqliteDatabase object as parameter which is the java representation of the database.

The SqliteOpenHelpher class provides the getReadleDatabase() and getWritableDatabase() methods to get access to an SqliteDatabase object either in read or write mode.

Helping classes in SqliteDatabase

Content Values:

The object ContentValues allows to define key/values. The key represents the table column name and the Value represents the content for the table record in this column.Content Values can be used for insert and updates of database entries.

Cursor:

When a query fires in sqlite database it returns a Cursor object. A cursor represents the result of a query and basically points to one row of the query result. This way Android retrieve the result of query. It does not have to load all data into memory.

Cursor class has many methods: Here is some of that

getCount() — It gives the number of rows resultant query.

moveToFirst() -To move cursor first rows .

moveToLast() — To move the cursor to last row.

moveToNext() — To move the cursor in next row.

OUTPUT

You can find the complete project code on GitHub .

Fork us on Github

Don’t forgot to share .Let’s Learn Together.

You may find us on our Blog

--

--