我正在编写可能是创建游标的最简单程序,因此我可以演示查询。我陷入了错误“无法解析符号getReadableDatabase()”
PS:我已经准备好尝试使缓存无效并重新启动。
package com.example.arjunrao.databasedemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.database.Cursor;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onclick(View view){
EditText editText = (EditText) findViewById(R.id.editText);
String drink_name = (String)editText.getText().toString();
try{
SQLiteOpenHelper database = new Database_class(this);
SQLiteDatabase db = new database.getReadableDatabase();
db.query("DRINK",new String[] {"NAME","DESCRIPTION","FAVORITE"});
}catch(SQLiteException ex){
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("SQLITE EXCEPTION GENERATED");
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
}
}
}
最佳答案
必须是您代码中的new
。更改此行:
SQLiteOpenHelper database = new Database_class(this);
SQLiteDatabase db = new database.getReadableDatabase();
至
SQLiteOpenHelper database = new Database_class(this);
SQLiteDatabase db = database.getReadableDatabase();
您的IDE应该指出了这个错误,但是您的代码应该已经建立了错误。