Android sqlite:如何从特定列检索特定数据? [英] Android sqlite: how to retrieve specific data from particular column?

查看:60
本文介绍了Android sqlite:如何从特定列检索特定数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing restaurant menu application. My app has a sqlite table which has these columns:

"id", "category", "item_name"

类别列的内容为字符串类型.表的主键是 id .

Contents of category column is of type string. Primary key of table is id.

我要检索特定类别的数据.

I want to retrieve data of particular category.

例如,我想检索类别为 Veg 的所有商品的商品名称,然后在列表视图中显示此结果.

For example, I want to retrieve item names of all items which are in category Veg and then display this result in list view.

我尝试了以下不同的查询,但是都无法正常工作.请帮助我.

I have tried following different queries but both are not working. Please help me.

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;

推荐答案

尝试一下:

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

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

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

这篇关于Android sqlite:如何从特定列检索特定数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆