Christian
Christian

Reputation: 7320

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
    }

}

Upvotes: 4

Views: 3743

Answers (2)

user
user

Reputation: 87064

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 
}

Upvotes: 10

Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10083

You dont actually need to extend that its already available for you since you import sqliteDatabase class.
Simple define:

Sqlitedatabase dbname;
dbname.execSQL(Query);

Or:

public class MySQLiteHelper extends SQLiteOpenHelper {

  public static final String TABLE_COMMENTS = "comments";
  public static final String COLUMN_ID = "_id";
  public static final String COLUMN_COMMENT = "comment";

  private static final String DATABASE_NAME = "commments.db";
  private static final int DATABASE_VERSION = 1;

  // Database creation sql statement
  private static final String DATABASE_CREATE = "create table "
      + TABLE_COMMENTS + "(" + COLUMN_ID
      + " integer primary key autoincrement, " + COLUMN_COMMENT
      + " text not null);";

  public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(MySQLiteHelper.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
    onCreate(db);
  }
} 

Upvotes: 0

Related Questions