Android sqlite:如何从特定列中检索特定数据?

时间:2011-09-12 11:58:16

标签: android sqlite

我正在开发餐厅菜单应用程序。我的应用程序有一个sqlite表,其中包含以下列:

"id", "category", "item_name"

类别列的内容是string类型。表的主键是id

我想检索特定类别的数据。

例如,我想检索类别Veg中所有项目的项目名称,然后在列表视图中显示该结果。

我尝试过不同的查询,但两者都无效。请帮帮我。

String vg ="Veg";
Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
           KEY_ROWID, KEY_CATEGORY, KEY_NAME,KEY_PRIZE,KEY_DESCRIPTION },
        KEY_CATEGORY + "=" + vg , null, null, null,null, null);
        if (mCursor != null) {              
                        mCursor.moveToFirst();
        }
        return mCursor;

原始查询

String query = "SELECT * FROM todo WHERE category =" + vg ;
          Cursor  cursor = database.rawQuery(query,null);
            if (cursor != null) {
                cursor.moveToFirst();
            }
            return cursor;

9 个答案:

答案 0 :(得分:14)

试试这个:

String query = "SELECT * FROM todo WHERE category='" + vg;

Cursor  cursor = database.rawQuery(query,null);

        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;

答案 1 :(得分:11)

String query = "SELECT item_name FROM todo WHERE category =" + vg ;
Cursor  cursor = database.rawQuery(query,null);
if (cursor.moveToFirst()) {
         while (cursor.isAfterLast() != true) {
           string itemname =  cursor.getString(cursor.getColumnIndex("item_name")));
          }
}

这里moveToFirst检查是否有符合条件的项目,然后使用while循环遍历游标。字符串itemname可以由list adaper替换以填充数据。希望这会有所帮助。

答案 2 :(得分:6)

dbHelper = new DBHelper(getApplicationContext());
SQLiteDatabase db = dbHelper.getReadableDatabase();

Cursor cursor = db.rawQuery("select * from centuaryTbl where email='"+email+"'",null);
if (cursor.moveToFirst())
{
    do
    {
        String s1 = cursor.getString(cursor.getColumnIndex("s1"));
        String s2 = cursor.getString(cursor.getColumnIndex("s2"));
        String s3 = cursor.getString(cursor.getColumnIndex("s3"));


    }while (cursor.moveToNext());
}

答案 3 :(得分:2)

String query = "SELECT * FROM Table_Name WHERE Col_Name='name'";
Cursor cursor = mDb.rawQuery(query, null);

答案 4 :(得分:1)

试试这个:

String vg ="Veg";
return database.rawQuery("SELECT * FROM subjects where " + KEY_CATEGORY + " = ?",
                            new String[]{vg});

答案 5 :(得分:0)

  String q="SELECT * FROM todo WHERE category='" + vg +"'";
    SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = null;
        cursor = db.rawQuery(q, null);
        if (cursor.moveToFirst()) {
            do {

            } while (cursor.moveToNext());

        }

答案 6 :(得分:0)

String selectQuery = "select * from " + TABLE;
        sqlDatabase = this.getWritableDatabase();

     Cursor cursor = sqlDatabase.rawQuery(selectQuery, null);

    if (cursor != null) {
        cursor.moveToFirst();
    }


    return cursor;

这会对你有帮助..

答案 7 :(得分:0)

public List<String> getAllData(String email)
{
    db = this.getReadableDatabase();
    String[] projection = {email};

    List<String> list = new ArrayList<>();

   Cursor cursor = db.query(Table_name,null,"email=?",projection,null,null,null,null);

  //  cursor.moveToFirst();

    if(cursor.moveToFirst()) {
        do {

            list.add(cursor.getString(cursor.getColumnIndex("id")));
            list.add(cursor.getString(cursor.getColumnIndex("name")));
            list.add(cursor.getString(cursor.getColumnIndex("email")));
            list.add(cursor.getString(cursor.getColumnIndex("password")));
           // cursor.moveToNext();

        } while (cursor.moveToNext());
    }
    return list;
}

答案 8 :(得分:0)

您可以在

上传递过滤器的值
a(1, a(2, :))