我想扩展SQLiteDataBase类,以便能够覆盖一些方法(比如rawQuery,execSQL,...),并且可以查看有关查询执行时间的一些统计信息。
我称这些功能有数百个地方。因此,从基类SQLiteDatabase创建派生类将对我有所帮助!
问题是:在扩展SQLiteDataBase时,它无法从超类中找到任何构造函数。
import android.database.sqlite.SQLiteDatabase;
public class MySQLiteDatabase extends SQLiteDatabase
{
MySQLiteDatabase()
{
super(); // <<<--- can't find any visible constructor
}
}
答案 0 :(得分:10)
我称之为功能的地方有数百个。所以, 从基类SQLiteDatabase创建派生类将有所帮助 我很多!。
您无法扩展SQLiteDatabase
,因为它已声明为final
,您无法在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
}
答案 1 :(得分: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);
}
}