Friday, April 26, 2024

Android java: using SQLiteOpenHelper instead Room Library

Using additional Room may increase size of application, means more lines required to executed. I preferred to implement SQLiteOpenHelper rather then using Room Library.

Here is code for extending SQLiteOpenHelper:

package com.dedetok.radiowalkman.mydb;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

/*
 * added 20240319
 * Async task will be done in DB Repository to access database (RUD)
 */

public class MyDBHelper extends SQLiteOpenHelper {

    final static String dbName = "myradiolist"; // db name must final static
    final static int dbversion = 2; // db version must final static

    public MyDBHelper(@Nullable Context appContext) {
        //
        super(appContext, dbName, null, dbversion);
    }

    /*
     * Only called once after creating a new database
     * Will not called if database exist (upgrade version)
     */
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        //Log.e("dedetok","onCreate"); //debug
        createMyDB(sqLiteDatabase);
    }

    /*
     * Only called once if database exist and version number increase (upgrade)
     */
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        // upgrade from 1 to 2
        //Log.e("dedetok","onUpgrade "+oldVersion+" to "+newVersion); //debug
        if (oldVersion==1) {
            upgradeDBV1toV2(sqLiteDatabase);
        }

    }

    /*
     * version 1
     */
    private void createMyDB(SQLiteDatabase sqLiteDatabase) {
        //Log.e("dedetok","createMyDB"); //debug
        // Main table radiolist
        String sqlCreateRadioList = "CREATE TABLE IF NOT EXISTS "+
                "radiolist ("+
                "countryname TEXT NOT NULL,"+
                "cityname TEXT NOT NULL,"+
                "radiostation TEXT NOT NULL,"+
                "logourl TEXT NOT NULL,"+
                "streamurl TEXT NOT NULL,"+
                "PRIMARY KEY(countryname, cityname, radiostation));";
        sqLiteDatabase.execSQL(sqlCreateRadioList);
        // table radiolistversion only has 1 record and one column
        String sqlCreateRadioListVersion = "CREATE TABLE IF NOT EXISTS "+
                "radiolistversion ("+
                "listversion TEXT NOT NULL,"+
                "PRIMARY KEY(listversion));";
        sqLiteDatabase.execSQL(sqlCreateRadioListVersion);

        //Log.e("dedetok", "create table app_preferences"); //debug
        // 20240422
        upgradeDBV1toV2(sqLiteDatabase);

    }

    /*
     * version 1 to version 2
     */
    private void upgradeDBV1toV2(SQLiteDatabase sqLiteDatabase) {
        //Log.e("dedetok", "upgradeDBV1toV2"); //debug
        // 20240422
        String sqlCreatePreferences = "CREATE TABLE IF NOT EXISTS "+
                "app_preferences ("+
                "pref_key TEXT NOT NULL,"+
                "pref_value TEXT NOT NULL,"+
                "PRIMARY KEY(pref_key));";
        sqLiteDatabase.execSQL(sqlCreatePreferences);
    }

}

 This is java class in Radio Walkman application.

No comments:

Post a Comment