我是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) {

    }
}

最佳答案

在初始化父构造函数之前,不能引用此变量。当将此变量添加到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;

07-26 08:48