简体   繁体   English

如何扩展SQLiteDatabase类?

[英]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. 我想扩展SQLiteDataBase类,以便能够覆盖一些方法(比如rawQuery,execSQL,...),并且可以执行一些关于查询执行时间的统计信息。

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! 因此,从基类SQLiteDatabase创建派生类将对我有所帮助!

The problem is: when extending SQLiteDataBase, it can't find any constructor from the super class. 问题是:在扩展SQLiteDataBase时,它无法从超类中找到任何构造函数。

import android.database.sqlite.SQLiteDatabase;

public class MySQLiteDatabase extends SQLiteDatabase
{

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

}

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!. 因此,从基类SQLiteDatabase创建派生类将对我有很大帮助!

You can't extend the SQLiteDatabase as it's declared final and you can't extend a final class in java. 您无法扩展SQLiteDatabase因为它已声明为final ,您无法在java中扩展final类。

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: 另一种方法是创建一个“包装器” SQLiteDatabase ,它将使用其方法复制SQLiteDatabase类,并在包装​​器方法中插入测量逻辑:

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 
}

You dont actually need to extend that its already available for you since you import sqliteDatabase class. 您实际上不需要扩展它已经可用的扩展,因为您导入了sqliteDatabase类。
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);
  }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM