SQLite is the data repository in Android where we can store the data in the user's device and use it anytime when required. In this Blog, we will take a look at creating an SQLite database in an Android app and adding that data to the database in an Android app.
SQLite Database is an open-source database provided in Android which is used to store data inside the user’s device. We can execute so many operations on this data such as adding new data, updating, reading, and deleting data. SQLite is an offline database that is locally stored in the user’s device and we do not have to create any link to connect to this database.
check out : Sql basics
Step by Step Implementation
Step 1: Create a New Project
Step 2: Adding permissions to access the storage in the AndroidManifest.xml file
Step 3: Working with the activity_main.xml file
Step 4: Creating a new Java class for performing SQLite operations
Step 5: Working with the MainActivity.java file
Creating And Updating Database In Android
For creating, updating, and other operations you require to create a subclass or SQLiteOpenHelper class. SQLiteOpenHelper is a helper class to manage database creation and version management. It provides two methods onCreate(SQLiteDatabase DB), and onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion).
The SQLiteOpenHelper is responsible for the opening database if exists, creating the database if it does not exists, and upgrading it if required. The SQLiteOpenHelper only requires the DATABASE_NAME to create a database. After extending SQLiteOpenHelper you will need to implement its methods onCreate, onUpgrade, and constructor.
onCreate(SQLiteDatabase sqLiteDatabase) method is called only once throughout the application lifecycle. It will be called whenever there is the first call to getReadableDatabase() or getWritableDatabase() function available in the super SQLiteOpenHelper class. So SQLiteOpenHelper class calls the onCreate() method after creating a database and instantiate SQLiteDatabase object. The database name is passed in constructor call.
onUpgrade(SQLiteDatabase db,int oldVersion, int newVersion) is only called whenever there is a updation in the existing version. So to update a version we have to increment the value of version variable passed in the superclass constructor.
In onUpgrade method we can write queries to perform whatever action is required. In the most example you will see that existing table(s) are being dropped and onCreate() method is being called to create tables again. But it’s not mandatory to do so and it all depends upon your requirements.
We have to change database version if we have added a new row in the database table. If we have requirement that we don’t want to lose existing data in the table then we can write alter table query in the onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) method.
- getColumnNames() This method is used to get the Array of column names of our SQLite table.
- getCount() This method will return the number of rows in the cursor.
- isClosed() This method returns a Boolean value when our cursor is closed.
- getColumnCount() This method returns the total number of columns present in our table.
- getColumnName(int columnIndex) This method will return the name of the column when we passed the index of our column in it.
- getColumnIndex(String columnName) This method will return the index of our column from the name of the column.
- getPosition() This method will return the current position of our cursor in our table.