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

时间:2015-07-30 18:48:01

标签: java android database sqlite

大家好,所以这是我的问题,我需要能够访问创建的行的唯一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;
}

HERES MY TABLE:

    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);
    }

1 个答案:

答案 0 :(得分:0)

更新您的查询,因为您的查询在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;
    }