Reputation: 5480
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.
Upvotes: 0
Views: 693
Reputation: 6220
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);
}
}
Upvotes: 1