如何在androidstudio中正确使用database.raw查询?

mbyulnm0  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(225)

我一直在尝试制作一个健身应用程序,它将通过我制作的数据库进行解析,并从我的数据表中提取特定的值。我的表格有经验值(初学者或有经验者)、设备值(负重或无负重)和训练值(例如:俯卧撑)。如果用户是初学者,并且想要重量或任何其他条件,我可以使用这些参数,那么我想根据这些条件来获取训练。我制作了一个数据库助手,并且有使用database.raw query的代码,但是每次我尝试从练习数据表(我的数据表的名称)中选择设备和经验进行查询时,它都会给我相同的错误。下面是eh database helper的代码和我正在引用的特定方法(getalldata)。同时也显示了错误。任何帮助都很好,谢谢

public static final String TABLE_EXERCISES = "exercises";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_EXPERIENCE = "experience";
    public static final String COLUMN_EQUIPMENT = "equipment";
    public static final String COLUMN_WORKOUT = "workout";
    public static final String DATABASE_NAME = "exercises.db";
    private static final int DATABASE_VERSION = 1;

    // Database creation sql statement

    public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL("create table " + TABLE_EXERCISES +" (_id INTEGER PRIMARY KEY AUTOINCREMENT,experience TEXT, equipment TEXT,workout TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(MySQLiteHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_EXERCISES);
        onCreate(db);
    }
    public boolean insertData(String experience, String equipment, String workout){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_EXPERIENCE, experience);
        contentValues.put(COLUMN_EQUIPMENT, equipment);
        contentValues.put(COLUMN_WORKOUT, workout);
        long result = db.insert(TABLE_EXERCISES, null, contentValues);
        if(result == -1)
            return false;
        else
            return true;
    }
    public Cursor getAllData(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("SELECT equipment, experience FROM exercises WHERE experience = ? AND equipment = ?", new String[] {"beginner", "weights"});
        //Cursor res = db.rawQuery("select * from "  + TABLE_EXERCISES, null);
        if (res.moveToFirst()) {
            return res;
        }
       return null;
    }
}

----------
Error:

E/CursorWindow: Failed to read row 1, column 2 from a CursorWindow which has 140 rows, 2 columns.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.hfad.workoutmaker, PID: 32245
    java.lang.IllegalStateException: Couldn't read row 1, col 2 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.database.CursorWindow.nativeGetString(Native Method)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题