如何使用带有WHERE子句的query()?

时间:2015-10-12 20:12:21

标签: android sql database where

我需要提出问题,列:活动,课程和屏幕等于例如:“1”,“2”,“3”。 注意:问题也是一栏。

我的代码应该返回一个字符串=“4”但正在返回:

android.database.sqlite.SQLiteCursor@547e1a74

代码:

databaseHelper = Database.getInstance(getApplicationContext());

long id4 = databaseHelper4.insertData("1", "2", "3","4", "5", "6", "7", "8");
String question = databaseHelper4.getQuestion("1", "2", "2");
Message.message(IntroActivity.this, question);

数据库:

public String getQuestion(String activity, String lesson, String screen){

    SQLiteDatabase db = helper.getWritableDatabase();
    String[] columns = {DatabaseHelper.QUESTION};
    Cursor cursor = db.query(DatabaseHelper.TABLE_NAME, columns, DatabaseHelper.ACTIVITY +
    "=?" + " AND " + DatabaseHelper.LESSON + "=?" + " AND " + DatabaseHelper.SCREEN +
    "=?", new String[]{activity, lesson, screen}, null, null, null, null);

    return cursor.toString();

}

2 个答案:

答案 0 :(得分:1)

下面:

return cursor.toString();

Cursor.toString()方法返回Cursor的字符串表示,而不是Cursor包含的值。

从游标获取QUESTION列值:

cursor.moveToFirst();
String question = cursor.getString(cursor.getColumnIndex(
                                               DatabaseHelper.QUESTION));

答案 1 :(得分:1)

而不是返回cursor.toString(),您应该返回

cursor.getString(cursor.getColumnIndex(DatabaseHelper.QUESTION));

致电cursor.moveToFirst();

public String getQuestion(String activity, String lesson, String screen){

   SQLiteDatabase db = helper.getWritableDatabase();
   String[] columns = {DatabaseHelper.QUESTION};
   Cursor cursor = db.query(DatabaseHelper.TABLE_NAME, columns, DatabaseHelper.ACTIVITY +
   "=?" + " AND " + DatabaseHelper.LESSON + "=?" + " AND " + DatabaseHelper.SCREEN +
   "=?", new String[]{activity, lesson, screen}, null, null, null, null);

     cursor.moveToFirst()
     return cursor.getString(cursor.getColumnIndex(DatabaseHelper.QUESTION));;

}