Working with sqlite Database in Android

Published On: 24 January 2017.By .
  • Digital Engineering
  • Mobile

Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very light weight database which comes with Android OS. In this blog, I’ll be discussing how to write classes to handle all SQLite operations.

In this blog, I am taking an example of storing user contacts in SQLite database. I am using a table called peopleTable to store user hotness. This table contains three columns id (INT), persons_name (TEXT), persons_hotness(TEXT).

Writing Sqlite Database Handler Class

We need to write our own class to handle all database CRUD(Create, Read, Update, and Delete) operations.

1. Create a new project by going to File ⇒ New Android Project.
2. Once the project is created, create a new class in your project src directory and name it DbHelper.java ( Right Click on src/package ⇒ New ⇒ Class)
3. Now extend your DbHelper.java class from SQLiteOpenHelper.

4. After extending your class from SQLiteOpenHelper you need to override two methods onCreate() andonUpgrage()
onCreate() – These is where we need to write create table statements. This is called when database is created.
onUpgrade() – This method is called when database is upgraded like modifying the table structure, adding constraints to database etc.,

Open database 
  Close database 

Inserting new Record 

Retrieving single Row

Retrieving All Row(s)

Updating  Row(s)

Deleting Row(s)

Complete code looks like 

 SQLite use like below code

 

Related content

That’s all for this blog

Go to Top