Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend SQLiteDatabase class?

I'd like to extend SQLiteDataBase class to be able to override some methods (like rawQuery, execSQL, ...) with the purpouse of doing some statistics about queries execution times.

There are hundreds of places where I call those functions. So, creating a derived class from the base class SQLiteDatabase will help me a lot!

The problem is: when extending SQLiteDataBase, it can't find any constructor from the super class.

import android.database.sqlite.SQLiteDatabase;

public class MySQLiteDatabase extends SQLiteDatabase
{

    MySQLiteDatabase()
    {
        super(); // <<<--- can't find any visible constructor
    }

}
like image 883
Christian Avatar asked Dec 26 '22 07:12

Christian


1 Answers

There are hundreds of places where I call thoses functions. So, creating a derived class from the base class SQLiteDatabase will help me a lot!.

You can't extend the SQLiteDatabase as it's declared final and you can't extend a final class in java.

An alternative is to make a "wrapper" SQLiteDatabase which will replicate the SQLiteDatabase class with its methods and insert the measurement logic in the wrapper methods:

public class SQLiteDatabaseWrapper {

    private SQLiteDatabase mDatabase;

    public void initDatabase() {
      //obtain the database here and assign it to mDatabase 
    }

    // replicate the SQLiteDatabase methods 
    public void execSQL(String sql) {
       //insert whatever logic you have for measurement
       // do the measurement
       mDatabase.execSQL(sql); 
    }
    // other methods from SQLiteDatabase 
}
like image 159
user Avatar answered Jan 11 '23 05:01

user