Android 方法 openOrCreateDatabase(String, int, null) 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3488661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:26:14  来源:igfitidea点击:

The method openOrCreateDatabase(String, int, null) is undefined

android

提问by Saurabh Verma

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 :

当我在 Service 类中实现它时,此代码工作正常,但是当我尝试在 GeneraterThread 类的 onPostExecute 事件处理程序中实现它时,实现 AsyncTask,我收到以下错误:

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

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

采纳答案by Konstantin Burov

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

看起来您正在尝试在没有该方法的 GeneraterThread 实例上调用 openOrCreateDatabase 方法(而 Service 类具有该方法)。您可能会传入对 Context 对象的引用并调用其上的方法。或者使用 SQLiteDatabase.openOrCreateDatabase() 的静态方法。

回答by a4vi2r

It looks like your just set wrong arguments for function.

看起来您刚刚为函数设置了错误的参数。

There are these definitions in SDK:

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

也许你想打电话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

回答by Pentium10

Use it with your parent class

与您的父类一起使用

something like

就像是

myService.this.openOrCreateDatabase

回答by Ali Bagheri

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