Saurabh Verma
Saurabh Verma

Reputation: 6728

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

Upvotes: 7

Views: 45408

Answers (4)

Ali Bagheri
Ali Bagheri

Reputation: 3429

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);

Upvotes: 0

a4vi2r
a4vi2r

Reputation: 101

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

Upvotes: 7

Konstantin Burov
Konstantin Burov

Reputation: 69238

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().

Upvotes: 5

Pentium10
Pentium10

Reputation: 207982

Use it with your parent class

something like

myService.this.openOrCreateDatabase

Upvotes: 1

Related Questions