I'm following the Android developers tutorial to set up a database but I'm having an issue. This is my schema file :
public final class ContactsDBSchema {
public ContactsDBSchema(){};
public static abstract class ContactsEntry implements BaseColumns
{
public static final String TABLE_NAME = "contacts";
public static final String COLUMN_CONTACT__ID = "contact_id";
public static final String COLUMN_CONTACT__NAME = "contact_name";
public static final String COLUMN_CONTACT__PHONE = "contact_phone";
}
public class ContactsDBhelper extends SQLiteOpenHelper
{
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "contacts.db";
public String SQL_CREATE_ENTRIES = "create table" + ContactsEntry.TABLE_NAME + ContactsEntry.COLUMN_CONTACT__ID +"INTEGER PRIMARY KEY AUTOINCREMENT," + ContactsEntry.COLUMN_CONTACT__NAME + "TEXT," + ContactsEntry.COLUMN_CONTACT__PHONE + "TEX";
public String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS" + ContactsEntry.TABLE_NAME ;
public ContactsDBhelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
}
The issue now is when I'm trying to access it from another class like this one:
public class ContactsDBprocess {
ContactsDBSchema.ContactsDBhelper contactsHelper = new ContactsDBSchema.ContactsDBhelper(getContext());
}
I'm unable to access the helper class properly. The getContext()
function does not work and I get an error:
Cannot resolve method
getContext
I tried doing this:
ContactsDBSchema.ContactsDBhelper contactsHelper = new ContactsDBSchema.ContactsDBhelper(this)
ContactsDBSchema.ContactsDBhelper contactsHelper = new ContactsDBSchema.ContactsDBhelper(getApplicationContext())
I still don't have any luck. Any suggestions on how to solve this problem?