I know how to do SQLite Database, when doing it specifically in its own dedicated java class. But whenever i want it to be inside of something, in this case, an Intent Service, it always give me errors, and i can’t debug it.
package my.skul;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class CheckClassroom extends IntentService {
/**
* A constructor is required, and must call the super IntentService(String)
* constructor with a name for the worker thread.
*/
public CheckClassroom() {
super("CheckClassroom");
}
/**
* The IntentService calls this method from the default worker thread with
* the intent that started the service. When this method returns,
* IntentService stops the service, as appropriate.
*/
@Override
protected void onHandleIntent(Intent intent) {
}
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, "myschool", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
public CheckClassroom(Context c) {
ourContext = c;
}
public CheckClassroom open() {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
}
getColumns()
{
//retrieve sqlitedatabase data
}
}
I’ve been reading a lot about constructor, but i can’t really fully understand it, explaining all my errors here would really give me a better understanding of this. Thanks..
Because you declared the
DbHelperclass static, it will create an instance as soon as theCheckClassroomis instantiated. At that time, ourContext is still not initialized.To fix this, remove the static for the
DbHelperclass and make one instance to be a static field instead. Then in the constructor you can initialize that (after you initialize ourContext).And instead of using ourContext, you can just use
CheckClassroom.thisin theDbHelperclass.Also…
The
public CheckClassroom(Context c) { ourContext = c; }function has nothing to do inside theDbHelperclass….Also that open and close functions do not seem right, instead I think you should override onCreate and onDestroy
Here is your code, fixed: