如何使用查询从数据库中获取列值?

时间:2012-04-09 19:01:11

标签: android sql sqlite

我在使用带有android的sqlite数据库方面是全新的。 我想使用以下SQL语句从数据库中获取值:

"SELECT description FROM games WHERE _id =" + categoryID

其中categoryID是listView中单击元素的位置(也是_id列的值)。

listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,

                String categoryID = new Integer(position).toString();

                final String SELECT_FROM = "SELECT description " + "FROM games "
                        + "WHERE _id = " + categoryID;
                database.query(SELECT_FROM);

                Intent intent = new Intent();
                intent.setClass(SqliteDbActivity.this, ViewAction.class);

                Bundle b = new Bundle();
                b.putString("categoryID", SELECT_FROM);
                intent.putExtras(b);

                startActivity(intent);

            }
        });

我知道我应该使用database.query()但是我应该如何正确编写以前的sql语句来获取值?

表格如下:

_id description name
1    some1       name1
2    some2       name2       
3    some3       name3    

请帮助。

1 个答案:

答案 0 :(得分:2)

String categoryID = new Integer(position).toString();

            final String SELECT_FROM = "SELECT description " + "FROM games "
                    + "WHERE _id = " + categoryID;
            Cursor cursor = database.query(SELECT_FROM);
            cursor.moveToFirst();

            Intent intent = new Intent();
            intent.setClass(SqliteDbActivity.this, ViewAction.class);

            Bundle b = new Bundle();
            b.putString("categoryID", cursor.getString(cursor.getColumnIndex("fieldname"));
            intent.putExtras(b);

            startActivity(intent);

您的database.query(SELECT_FROM)将返回cursor,借助光标您可以开展工作