我是SQLite数据库的新手。我目前创建了一个数据库但是我的助手类中有两个错误,在DATABASE_NAME
常量和DATABASE_VERSION
常量的构造函数中。
错误说明:在调用超类型构造函数之前无法引用PetDbHelper.DATABASE_NAME
。
我对DATABASE_VERSION
常量有同样的错误。
PetDbHelper.java:
public class PetDbHelper extends SQLiteOpenHelper {
private final String DATABASE_NAME = "shelter.db";
private final int DATABASE_VERSION = 1;
public PetDbHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String SQL_CREATE_PETS_TABLE = "CRAETE TABLE" + PET_ENTRY.TABLE_NAME + "(" + PET_ENTRY._ID + "INTEGER PRIMARY KEY AUTOINCREMENT,"
+ PET_ENTRY.NAME + "TEXT NOT NULL," + PET_ENTRY.BREED + "TEXT," + PET_ENTRY.GENDER + "INTEGER NOT NULL,"
+ PET_ENTRY.WEIGHT + "INTEGER NOT NULL DEFAULT 0);";
sqLiteDatabase.execSQL(SQL_CREATE_PETS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
答案 0 :(得分:3)
在初始化父构造函数之前,不能引用此变量。将此变量添加到static
修饰符时,不会再报告错误。
更改强>
private final String DATABASE_NAME = "shelter.db";
private final int DATABASE_VERSION = 1;
到
private static final String DATABASE_NAME = "shelter.db";
private static final int DATABASE_VERSION = 1;