Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access database in non-Activity class

Tags:

android

sqlite

I have some class (SomeClass.class). I want to have some static methods in it like getAllDatabaseItems, getTableItems, insertNewRecord and so on.

If I do it this way

SQLiteDatabase db = openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null);
  • I need to extend Activity (but still can't use it in static methods) or pass a "db" variable in every single method (from "caller activity") which is pretty bulky.

What's the solution so I can from some class call SomeClass.getAllDatabaseItems()?

@MobileDev123 So I still need to extend Activity (because of the method openOrCreateDatabase)? If I have this class (which isn't actually an activity, I don't use it that way)

public class Partner extends Activity {
@SuppressWarnings("static-access")
public Partner(Context mContext) {
    myContext = mContext;
    db = openOrCreateDatabase(DATABASE_NAME, myContext.MODE_PRIVATE, null);

    db.execSQL("CREATE TABLE IF NOT EXISTS " + PARTNER_TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " VARCHAR);");
    db.execSQL("CREATE TABLE IF NOT EXISTS " + ADDRESS_TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, " + PARTNER_ID + " INT, " + ADDRESS + " VARCHAR, " + CITY + " VARCHAR);");
}

And then call it from some of my activites like this

    Partner newPartner = new Partner(this);
    partnersItems = newPartner.getAllItems();

I get an NullExceptionError on line 4 (Partner.class) - why? If I use static reference on

MODE_PRIVATE => (Context.MODE_PRIVATE)

again it's not working.

@Falmarri same with static, if I pass in "this" argument (from some caller class) and receive it as an Context argument in my static method still can't successfully create/open my database (see lines before)

like image 771
svenkapudija Avatar asked Dec 30 '10 00:12

svenkapudija


1 Answers

There must be an activity or service which calls your class, what you can do is pass this at your convenient time. (I prefer to pass it in constructor).

On receiving hand use the context instant.

E.g From MyActivity class you can call createDatabase(this) or new DataServices(this) but in DataServices class the argument type must be context instead of MyActivity.

Now you have the context parameter and you can use it in the way you want including calling openOrCreateDatabase() .

Edit : Adding the code

from Main.java

write

DataBase database = new DataBase(this); //This will pass an instance of main. Which is eventually the subclass of Context.java

In DataBase class: you don't need to extend activity there. In constructor defination

public DataBase(Context context); //If you are using eclipse and rely on some automated tools you can see something like Main main. But use these line, so you can call it from any activity or service by passing this.

define a field of Context class, and refer it to the context arg.

Like this.localContext= context;

And by using localContext variable you can call openOrCreateDataBase column.

ADDITION: If you have any control (subclass of view) attached to this you can instantiate DataBase by calling new DataBase(view.getContext());

I hope this will help you.... in case any more help needed feel free to comment below.

like image 92
Prasham Avatar answered Sep 24 '22 13:09

Prasham