无法解析符号getReadableDatabase();

时间:2016-10-18 17:28:04

标签: android android-sqlite

我写的可能是创建Cursor的最简单的程序,所以我可以演示一个查询。我遇到了错误"无法解析符号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();
                }
            });


        }

    }
}

1 个答案:

答案 0 :(得分:3)

必须是代码中的new。改变这一行:

SQLiteOpenHelper database = new Database_class(this);
SQLiteDatabase db = new database.getReadableDatabase();

SQLiteOpenHelper database = new Database_class(this);
SQLiteDatabase db = database.getReadableDatabase();

您的IDE应该已经指出了这个错误,但是您的代码应该构建有错误。