2013-03-22 62 views
4

我想擴展SQLiteDataBase類,以便能夠覆蓋一些方法(如rawQuery,execSQL,...)與purpouse做一些關於查詢執行時間的統計信息。如何擴展SQLiteDatabase類?

有數百個地方我稱這些功能。所以,從基類SQLiteDatabase創建一個派生類將會幫助我很多!

問題是:當擴展SQLiteDataBase時,它無法從超類中找到任何構造函數。

import android.database.sqlite.SQLiteDatabase; 

public class MySQLiteDatabase extends SQLiteDatabase 
{ 

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

} 
+4

A ** **最終像類'SQLiteDatabase' **不能**延長。另外,請不要在Android的前面添加問題標題,底部的標籤就足夠了。 – Luksprog 2013-03-22 18:24:46

回答

10

有上百種,我稱之爲放入系統功能的地方。所以, 從基類SQLiteDatabase創建派生類將幫助 我很多!

不能擴展,因爲它是宣佈finalSQLiteDatabase,你也不會在Java擴展final類。

另一種方法是使一個「包裝」 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 
} 
0

你實際上並不需要擴展它已經可用於你,因爲你導入sqliteDatabase類。
簡單的定義:

Sqlitedatabase dbname; 
dbname.execSQL(Query); 

或者:

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); 
    } 
} 
+0

請不要使用您的文本的代碼格式。 – Sam 2013-03-22 18:30:38