当前位置: 代码迷 >> java >> 当我尝试在SQLite表中获取_id时,应用程序崩溃了吗?
  详细解决方案

当我尝试在SQLite表中获取_id时,应用程序崩溃了吗?

热度:105   发布时间:2023-07-25 19:13:13.0

嗨,大家好,这是我的问题,我需要能够访问所创建行的唯一ID,因为我创建了第二个表,该表将通过ID“连接”。 但是,这是我的问题,每当我尝试调用此方法时,我的应用程序就会崩溃。我希望社区可以在这里帮助我。 非常感谢。

public void addExerciseToDatabase(Exercises exercises){

    SQLiteDatabase db = myDBHelper.getWritableDatabase();

    ContentValues values = new ContentValues();
    ContentValues nuValues = new ContentValues();

    values.put(myDBHelper.COLUMN_BODYPARTNAME, exercises.get_bodyPart());
    values.put(myDBHelper.COLUMN_EXERCISENAME, exercises.get_exerciseName());

    long ID = db.insert(myDBHelper.TABLE_EXERCISES, null, values);

    //FOR THE NEW TABLE VALUES
    nuValues.put(myDBHelper.COLUMN_EXERCISENAME_ID, ID);
    nuValues.put(myDBHelper.COLUMN_NUMSETS, exercises.get_numSets());
    nuValues.put(myDBHelper.COLUMN_NUMWEIGHT, exercises.get_numWeight());
    nuValues.put(myDBHelper.COLUMN_NUMREPS, exercises.get_numReps());

    db.insert(myDBHelper.TABLE_EXERCISES_VALUE, null, nuValues);
    db.close();
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
THIS method returns the EXERCISE ID, that corresponds to the exercise
passed and Bodypart passed
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
public int getExerciseID(String exercise, String bodyPart) {

    SQLiteDatabase db = myDBHelper.getWritableDatabase();

    String query = "SELECT " + myDBHelper.COLUMN_ID + " FROM " + myDBHelper.TABLE_EXERCISES + " WHERE " +
            myDBHelper.COLUMN_BODYPARTNAME + " = " + bodyPart + " AND " +
            myDBHelper.COLUMN_EXERCISENAME + " = " + exercise ;

    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();
    int exerciseID = Integer.parseInt(c.getString(0));
    /*int exerciseID = c.getInt(c.getColumnIndex(myDBHelper.COLUMN_ID));*/
    c.close();
    db.close();

    return exerciseID;
}

在我的桌子上:

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "exercises.db";

    public static final String TABLE_EXERCISES = "exercises";
    public static final String TABLE_EXERCISES_VALUE = "exercises_value";

    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_BODYPARTNAME = "bodypartname";
    public static final String COLUMN_EXERCISENAME = "exercisename";

    public static final String COLUMN_EXERCISENAME_ID = "exerciseid";
    public static final String COLUMN_NUMSETS = "numsets";
    public static final String COLUMN_NUMWEIGHT = "numweight";
    public static final String COLUMN_NUMREPS = "numreps";


    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_EXERCISES + "(" +
                COLUMN_ID  + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_BODYPARTNAME + " TEXT NOT NULL," +
                COLUMN_EXERCISENAME + " TEXT NOT NULL" +
                ");";

        db.execSQL(query);

        query = "CREATE TABLE " + TABLE_EXERCISES_VALUE + "(" +
                COLUMN_ID  + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_EXERCISENAME_ID + " INTEGER NOT NULL," +
                COLUMN_NUMSETS + " INTEGER NOT NULL," +
                COLUMN_NUMWEIGHT + " INTEGER NOT NULL," +
                COLUMN_NUMREPS + " INTEGER NOT NULL" +
                ");";

        db.execSQL(query);
    }

更新查询,因为查询中SELECT和FROM中不包含空格

String query = "SELECT " + myDBHelper.COLUMN_ID + " FROM " + myDBHelper.TABLE_EXERCISES + " WHERE " +
            myDBHelper.COLUMN_BODYPARTNAME + " = " + bodyPart + " AND " +
            myDBHelper.COLUMN_EXERCISENAME + " = " + exercise ;

您也可以发布您的logcat错误。

    public int getExerciseID(String exercise, String bodyPart) {
        int exerciseID = 0;
        SQLiteDatabase db = myDBHelper.getWritableDatabase();

        String query = "SELECT " + myDBHelper.COLUMN_ID + " FROM " + myDBHelper.TABLE_EXERCISES + " WHERE " +
                myDBHelper.COLUMN_BODYPARTNAME + " = " + bodyPart + " AND " +
                myDBHelper.COLUMN_EXERCISENAME + " = " + exercise ;

        Cursor c = db.rawQuery(query, null);
        if(c.getCount()>0){
          c.moveToFirst();
          exerciseID = Integer.parseInt(c.getString(0));
          /*exerciseID = c.getInt(c.getColumnIndex(myDBHelper.COLUMN_ID));*/
        }
        c.close();
        db.close();

        return exerciseID;
    }
  相关解决方案