Sabre
Sabre

Reputation: 4183

How to get column value from database using query?

I'm completely new in using sqlite databases with android. I want to get value from database using following SQL statement:

"SELECT description FROM games WHERE _id =" + categoryID

Where categoryID is the position of clicked element in listView (which is also the value of _id column).

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);

            }
        });

I know that I should use database.query() but how should I correctly write the previous sql statement to get value?

Table looks like:

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

Please, help.

Upvotes: 0

Views: 6915

Answers (1)

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);

your database.query(SELECT_FROM) will return cursor, with the help of cursor you can do your work

Upvotes: 2

Related Questions