Amin soley
Amin soley

Reputation: 527

Have issues in a SQLite helper class

I am new in SQLite database. I currently created a database but I have two errors in my helper class inside the constructor for the DATABASE_NAME constant and the DATABASE_VERSIONconstant. The error says:Cannot reference PetDbHelper.DATABASE_NAME before supertype constructor has been called. I have the same error for DATABASE_VERSION constant. 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) {

    }
}

Upvotes: 1

Views: 143

Answers (1)

KeLiuyue
KeLiuyue

Reputation: 8237

This variable cannot be referenced until the parent constructor function is initialized. When you add this variable to the static modifier, no more errors are reported .

Change

private final String DATABASE_NAME = "shelter.db";
private final int DATABASE_VERSION = 1;

to

private static final String DATABASE_NAME = "shelter.db";
private static final int DATABASE_VERSION = 1;

Upvotes: 3

Related Questions