如何执行只从列中获取一个值的查询?

时间:2015-07-30 09:46:38

标签: android database sqlite

我正在尝试从我在SQLite中创建的表中检索唯一的_id。但它似乎并不喜欢它,我不知道我在这里做错了什么,任何建议都会受到赞赏

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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);
    /*return exerciseID= c.getInt(c.getColumnIndex(myDBHelper.COLUMN_ID));*/
    int exerciseID = -1;

    c.moveToFirst();
    while (!c.isAfterLast()) {
        if (c.getString(c.getColumnIndex(myDBHelper.COLUMN_ID)) != null) {
            exerciseID= c.getInt(c.getColumnIndex(myDBHelper.COLUMN_ID));
        }
        c.moveToNext();
    }

    return exerciseID;
}

3 个答案:

答案 0 :(得分:0)

为什么要在查询字符串中提供 \“”。逃避角色不是必需的。

我不清楚您的查询。但你可以尝试查询这样的东西。

String query="select distinct EXERCISE_ID from **yourTableName** where **your_condition** limit 1

答案 1 :(得分:0)

你可以尝试这样来获得特定的运动ID,

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);
    /*return exerciseID= c.getInt(c.getColumnIndex(myDBHelper.COLUMN_ID));*/
    int exerciseID = -1;

    c.moveToFirst();
    exerciseID = c.getInt(0);
    c.close();
    db.close();

    return exerciseID;
}

如果仍然有错误,请更改 - > myDBHelper.getWritableDatabase()到myDBHelper.getReadableDatabase();

干杯..!

答案 2 :(得分:0)

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

        // dataBaseGigaset = new SqliteDatabaseHelper(mainActivity);
        Cursor c1 = null;
        Cursor referenceDataCursor = null;
        try {
            db.open();
            referenceDataCursor = sqlitedb.query(myDBHelper.TABLE_EXERCISES, null, null, null, null, null, null);
            c1 = sqlitedb.rawQuery(query, null);
            referenceDataCursor.moveToFirst();
            int numRows = referenceDataCursor.getCount();
            if (numRows > 0) {
                /* Cursor to get Short Description */
                int count = c1.getCount();
                if (count == 0) {
                    return null;
                } else {
                    while (c1.moveToNext()) {
// get your value here. eg: c1.getString(0).toString().trim()

                    }
                }
                if ((referenceDataCursor != null)
                        && (!referenceDataCursor.isClosed())) {
                    referenceDataCursor.close();
                }
                if ((c1 != null) && (!c1.isClosed())) {
                    c1.close();
                }
            } else {
                if ((referenceDataCursor != null)
                        && (!referenceDataCursor.isClosed())) {
                    referenceDataCursor.close();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if ((referenceDataCursor != null)
                    && (!referenceDataCursor.isClosed())) {
                referenceDataCursor.close();
            }
            if ((c1 != null) && (!c1.isClosed())) {
                c1.close();
            }
            db.open();
        }

试试这段代码。希望这能解决问题。