[Solved] The method openOrCreateDatabase(String, int, null) is undefined

I’m trying to open database as follows :

SQLiteDatabase myDatabase;
myDatabase = openOrCreateDatabase("sudoku.db", Context.MODE_PRIVATE, null);

This code works fine when I implement it in the Service class, but when I try to implement this in the onPostExecute eventhandler of the GeneraterThread class,implementing AsyncTask, I get the following error :

The method openOrCreateDatabase(String, int, null) is undefined for the type GeneraterThread

Solution #1:

It looks like you’re trying to invoke openOrCreateDatabase method on GeneraterThread instance which doesn’t have the method (and Service class has the method). You probably may pass in a reference to a Context object and invoke the method on it. Or use static method of SQLiteDatabase.

openOrCreateDatabase().

Solution #2:

It looks like your just set wrong arguments for function.

There are these definitions in SDK:

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)

But your calling that function is wrong.

Maybe you wanted to call openDatabase (String path, SQLiteDatabase.CursorFactory factory, int flags)?

In this case you just set arguments in wrong order – you’re doing

openOrCreateDatabase("sudoku.db", Context.MODE_PRIVATE, null); //WRONG

instead:

openDatabase("sudoku.db", null, Context.MODE_PRIVATE); //RIGHT
Respondent: a4vi2r
Solution #3:

Use it with your parent class

something like

myService.this.openOrCreateDatabase
Respondent: Pentium10
Solution #4:

its better use this,

db = SQLiteDatabase.openOrCreateDatabase(DATABASE_PATH_AND_NAME, null);
db.close();
db = SQLiteDatabase.openDatabase(DATABASE_PATH_AND_NAME, null, Context.MODE_APPEND);
Respondent: Ali Bagheri
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.