0

I am following this tutorial made by our folks over at Google.

If you look at the third code snippet, the tutorial shows the FeedReaderDbHelper class which extends from the SQLiteOpenHelper class.

public class FeedReaderDbHelper extends SQLiteOpenHelper

Then, it shows how to create an instance of the FeedReaderDbHelper object in order to use it to read and write to the database.

FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());

The problem as I see it is that SQLiteOpenHelper is an abstract class. So whenever I do the following in my own program:

MatchEntry.MatchDBHelper dbHelper = new MatchEntry.MatchDBHelper(context);

I get an error: ....MatchContract.MatchEntry is not an enclosing class

Is this because of SQLiteOpenHelper being abstract? The way I have set-up my classes are exactly the same and pretty much mimic exactly what the docs have.

Update

This is how the structure looks like so far:

public class MatchContract {

    public MatchContract() {
    }

    public static abstract class MatchEntry implements BaseColumns {
        ...
        ...
        ...
    }

    public class MatchDBHelper extends SQLiteOpenHelper {
        ...
        ...
        ...
    }
}

Am I correct to put the MatchDBHelper inside the MatchContract class? I assume so as it needs to know the SQL_CREATE_TABLE string, otherwise it won't know.

2
  • 1
    MatchDBHelper can't be an inner class, I think. I also did that tutorial. Here you have it, take a look. You will see contract is separated from the sqlHelper class: github.com/algui91/CursoAndroid/tree/master/Ejemplos/… Commented May 3, 2014 at 19:18
  • @algui91 - Ahah ok. So will I have to create a DBHelper for every entry? For example: StudentDBHelper, ClassDBHelper, CarDBHelper? Commented May 3, 2014 at 19:25

1 Answer 1

1

The idea is, that a Contract class is an abstraction of the database, So, in MatchContract you will have all the global variables of the database and in the inner class, MatchEntry you will implement the abstraction of the database itself (columns etc). Then, with MatchDBHelper you will be managing the database (Creating it, upgrading it etc) And writing SQL entries.

As I said in the comments, You need to create the helper class as a normal class, not an inner class:

public class MatchContract {

public MatchContract() {
}

/// Global variables for the DB

    public static abstract class MatchEntry implements BaseColumns {
    // Structure of the database
    }
}


public class MatchDBHelper extends SQLiteOpenHelper {
    // Manage the database
}

As a complete example:

public final class PersonContract {

    public PersonContract() {
    }
    public static abstract class PersonEntry implements BaseColumns{
        public static final String TABLE_NAME = "person";
        public static final String COLUMN_NAME_ENTRY_ID = "personID";
        public static final String COLUMN_NAME_FIRST_NAME = "firstname";
        public static final String COLUMN_NAME_SECOND_NAME = "secondname";
    }
}

public class PersonDbBHelper extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "Persons.db";

    private static final String TEXT_TYPE = " TEXT";
    private static final String COMMA_SEP = ",";

    private static final String SQL_CREATE_ENTRIES =
            "CREATE TABLE " + PersonEntry.TABLE_NAME + " (" +
                    PersonEntry._ID + " INTEGER PRIMARY KEY," + // Heredado de BaseColumns
                    PersonEntry.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP +
                    PersonEntry.COLUMN_NAME_FIRST_NAME + TEXT_TYPE + COMMA_SEP +
                    PersonEntry.COLUMN_NAME_SECOND_NAME + TEXT_TYPE  +
                    " )";

    private static final String SQL_DELETE_ENTRIES =
            "DROP TABLE IF EXISTS " + PersonEntry.TABLE_NAME;


    public PersonDbBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you @algui91. Can you please tell me if i have to create a DBHelper for every entry? For example: StudentDBHelper, ClassDBHelper, CarDBHelper?
@Subby You need to create a DBHelper per database, not per table.
Ah right, so I will need to extend the SQL_CREATE_ENTRIES so that it includes ALL tables which I have?
@Subby I think it will be more organised have one SQL_CREATE per table, and then in onCreate do db.execSQL(SQL_CREATE_TABLE1); db.execSQL(SQL_CREATE_TABLE2);... I never create a database in android with more than one table, but I think this is a good approach.
One more question please. If the FeedEntry object extends from BaseColumns then why does it have it's own COLUMN_NAME_ENTRY_ID since the BaseColumns class has it's own ID?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.