无法解析符号 DatabaseHelper

Cannot resolve symbol DatabaseHelper

所以,我正在按照教程进行操作,并试图让它与我的 sql 数据库一起使用。所以,似乎没有错误,但这个红色单词(Gljive,它们是粗体)。应该有 class 名字还是什么?我输入了基本词(gljive,在我的语言中是蘑菇的意思)。我没有写我所有的专栏,因为如果这行不通,我不想把它们都写出来。我只想加载数据库,然后再加载填写特定微调器中的特定列。程序显示无法解析符号 Gljive。

package com.example.shromid;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class DatabaseHelper extends SQLiteAssetHelper {

//DB info
private static final String DB_NAME = "gljive.db";
private static final int DB_VERSION = 1;

//Table GLJIVE
public static final String TABLE_GLJIVE = "gljive";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAZIV = "Naziv";
public static final String COLUMN_KBOJA = "Klobuk_Boja";
public static final String COLUMN_KOBLIK = "Klobuk_Oblik";
public static final String COLUMN_KTEKSTURA = "Klobuk_Tekstura";
public static final String COLUMN_LBOJA = "Listici_Boja";
public static final String COLUMN_SOBLIK = "Strucak_Oblik";
public static final String COLUMN_SDNO = "Strucak_Dno";
public static final String COLUMN_MBOJA = "Meso_Boja";
public static final String COLUMN_MMIRIS = "Meso_Miris";
public static final String COLUMN_STANISTE = "Staniste";
public static final String COLUMN_SGRUPA = "Staniste_Grupa";
public static final String COLUMN_UPOTREBLJIVOST = "Upotrebljivost";

private static final String orderBy = DatabaseHelper.COLUMN_NAZIV + " ASC ";

private Context mContext;
private SQLiteDatabase mDB;

public DatabaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    mContext = context;
    setForcedUpgradeVersion();
}

public **Gljive** getGljiveById(int id) {
    **Gljive** gljive = null;

    SQLiteDatabase db = getReadableDatabase();

    Cursor cursor = db.query(
            DatabaseHelper.TABLE_GLJIVE, null, DatabaseHelper.COLUMN_ID + " = 1 ",
            new String[]{String.valueOf(1)}, null, null, orderBy);
    if (cursor != null) {
        cursor.moveToFirst();
        gljive = cursorToGljive(cursor);
        cursor.close();
    }

    return gljive;
}

public **Gljive** getGljiveByNaziv(String Naziv) {
    **Gljive** gljive = null;

    SQLiteDatabase db = getReadableDatabase();

    Cursor cursor = db.query(
            DatabaseHelper.TABLE_GLJIVE, null, DatabaseHelper.COLUMN_NAZIV + " 2",
            new String[]{name}, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        gljive = cursorToGljive(cursor);
        cursor.close();
    }

    return gljive;
}

public List<**Gljive**> getGljiveByKBoja(String Klobuk_Boja) {
    List<**Gljive**> gljive = new ArrayList<Gljive>();
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(
            DatabaseHelper.TABLE_GLJIVE, null, DatabaseHelper.COLUMN_KBOJA + " 3 ",
            new String[]{COLUMN_KBOJA}, null, null, orderBy);
    if (cursor != null) {
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Gljive gljive = cursorToGljive(cursor);
            gljive.add(gljive);
            cursor.moveToNext();
        }
        cursor.close();
    }

    return gljive;
}

private **Gljive** cursorToGljive(Cursor cursor){
    **Gljive** gljive= new Gljive();

    return gljive;
}
}

public Gljive getGljiveById(int id) {....}

是说定义一个名为 getGljiveById 的 public 方法,其中 returns 一个 Gljive 对象。

要拥有一个 Gljive 对象,您需要一个名为 Gljive

的 class

Is there supposed to be class name or what?

是的,所以创建一个名为 Gljive.java 的文件,然后有如下内容:-

public class Gljive {
    private long mId;
    private String mNaziv' // etc for all the properties (columns)

    // Constructor 
    public Gljive(String naziv, ) {
        this.mNaziv = naziv
    }

    // Setters/getters
    public long getId() {
        return this.mId;
    }
    public void setId(long id) {
        this.mId = id;
    }

    public String getNaziv() {
        return this.Naziv;
    }
    .... etc
}

使用上面的(缩短的)Gljive class 你可以在其他地方(例如在你的数据库助手中):-

//Create(construct) a Gljive instance
Gljive my_first_gljive = new Gljive("Button"); // Creates an instance using the constructor
my_first_gljive.setId(100); // Sets the mId for this instance to 100
String gljive_name_to_print = my_first_gljive.getNaziv();

然后您的大部分代码都需要正确地 accommodate/utilise Gljive 对象,例如 cursorToGljive 不能是 :-

private Gljive cursorToGljive(Cursor cursor){
    Gljive gljive= new Gljive();
    return gljive;
}

这将是(基于上面缩短的 Gljive):-

private Gljive cursorToGljive(Cursor cursor){
    Gljive gljive= new Gljive(
        cursor.getString(cursor.getColumnIndex(COLUMN_NAZIV)
    );
    gljive.setId(cursor.getColumnIndex(COLUMN_ID)); //Note(1)
    return gljive;
}

备注

  • (1) 你可以有一个允许传递 id 的构造函数(允许多个构造函数但具有不同的签名)。

使用上述 cursorToGljive 方法并更正 DatabaseHelper 中的其他方法可能存在的一些错误(参见评论):-

public Gljive getGljiveById(int id) {
    Gljive gljive = null;

    SQLiteDatabase db = getReadableDatabase();

    Cursor cursor = db.query(
            TABLE_GLJIVE, null, COLUMN_ID + "=?", //<<<< =? 
            new String[]{String.valueOf(id)},     //<<<< uses the passed id
            null, null, orderBy);
    if (cursor.moveToFirst()) {                   //<<<<< cursor will not be null
        gljive = cursorToGljive(cursor);
        cursor.close();
    }

    return gljive;
}

public Gljive getGljiveByNaziv(String Naziv) {
    Gljive gljive = null;

    SQLiteDatabase db = getReadableDatabase();

    Cursor cursor = db.query(
            TABLE_GLJIVE, null, COLUMN_NAZIV + "=?", //<<<< =?
            new String[]{Naziv},                     //<<<< use passed Naziv as argument
            null, null, null);
    if (cursor.moveToFirst()) {                      //<<<< cursor never null         
        gljive = cursorToGljive(cursor);
        cursor.close();
    }

    return gljive;
}

public List<Gljive> getGljiveByKBoja(String Klobuk_Boja) {
    List<Gljive> gljive = new ArrayList<>();
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(
            TABLE_GLJIVE, null, COLUMN_KBOJA + "=?",  //<<<< =?
            new String[]{Klobuk_Boja},                //<<<< use passed value as argument 
            null, null, orderBy);
    while (cursor.moveToNext()) {                     // compact loop removed null check
        gljive.add(cursorToGljive(cursor))            // add Gljive object to List
    }
    cursor.close();
    return gljive;
}

注意!这是原理代码,尚未经过测试,因此可能有错误。此外,它是一个缩短的版本,基本上是为了迎合 Naziv 专栏的需要。 Gljive.java应该修改为所有成员(属性),likewise/accordingly应该修改cursorToGljive