0

我在使用带有 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    

请帮忙。

4

1 回答 1

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,在光标的帮助下你可以做你的工作

于 2012-04-09T19:07:53.997 回答