开发者

Access database in non-Activity class

开发者 https://www.devze.com 2023-02-01 22:41 出处:网络
I have some class (SomeClass.class). I want to have some static methods in it like getAllDatabaseItems, getTableItems, insertNewRecord and so on.

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开发者_StackOverflow社区 my static method still can't successfully create/open my database (see lines before)


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.


You don't need a context. Use SQLiteDatabase static methods:

public static SQLiteDatabase openOrCreateDatabase (String path, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler)
public static SQLiteDatabase openOrCreateDatabase (String path, SQLiteDatabase.CursorFactory factory)
public static SQLiteDatabase openOrCreateDatabase (File file, SQLiteDatabase.CursorFactory factory)

or:


public static SQLiteDatabase openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags, DatabaseErrorHandler errorHandler)
public static SQLiteDatabase openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags)


I'm not sure it's possible to do this directly. I have struggled with this myself, but each application uses a different database handle, so Android needs to know how to grab it (a Context) when you get the handle from that oh so convenient:

SQLiteDatabase db = (SQLiteDatabase)(new SQLOpenHelperDerivedClass(this)).getReadableDatabase();

Perhaps you can figure out how go around SQLOpenHelper and use a hardcoded path to the database. There's probably a whole bunch of helpful stuff that SQLOpenHelper does for you that you'd be giving up though. I imagine that it's probably key to being cross device.


You need a context. If you want to do this in a static method, it has to take in a context

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号