TwoStarII
TwoStarII

Reputation: 351

Select Multiple Columns In Database

I would like to select multiple columns using the DatabaseHelper class, the following sql statement works on the database itself but not within android, only the first column is returned:

c = db.rawQuery("SELECT column1, column2, column3 FROM " + TABLE, null);

This only returns the first column but when executed in SQLite itself all columns are returned, am I missing something here?

Thanks

Upvotes: 3

Views: 6895

Answers (3)

Cillian Myles
Cillian Myles

Reputation: 704

For anyone still looking for an answer for this.

The approach of executing raw SQL statements is not really the desired approach. Sometimes when doing very very complex queries/joins etc. there may be a need to execute raw SQL, but I can't say that I have ever found myself in that situation.

The ideal approach would be using something like the following:

/**
 * Query the given table, returning a {@link Cursor} over the result set.
 *
 * @param table The table name to compile the query against.
 * @param columns A list of which columns to return. Passing null will
 *            return all columns, which is discouraged to prevent reading
 *            data from storage that isn't going to be used.
 * @param selection A filter declaring which rows to return, formatted as an
 *            SQL WHERE clause (excluding the WHERE itself). Passing null
 *            will return all rows for the given table.
 * @param selectionArgs You may include ?s in selection, which will be
 *         replaced by the values from selectionArgs, in order that they
 *         appear in the selection. The values will be bound as Strings.
 * @param groupBy A filter declaring how to group rows, formatted as an SQL
 *            GROUP BY clause (excluding the GROUP BY itself). Passing null
 *            will cause the rows to not be grouped.
 * @param having A filter declare which row groups to include in the cursor,
 *            if row grouping is being used, formatted as an SQL HAVING
 *            clause (excluding the HAVING itself). Passing null will cause
 *            all row groups to be included, and is required when row
 *            grouping is not being used.
 * @param orderBy How to order the rows, formatted as an SQL ORDER BY clause
 *            (excluding the ORDER BY itself). Passing null will use the
 *            default sort order, which may be unordered.
 * @return A {@link Cursor} object, which is positioned before the first entry. Note that
 * {@link Cursor}s are not synchronized, see the documentation for more details.
 * @see Cursor
 */
public Cursor query(String table, String[] columns, String selection,
        String[] selectionArgs, String groupBy, String having,
        String orderBy) {

    return query(false, table, columns, selection, selectionArgs, groupBy,
            having, orderBy, null /* limit */);
}

This method is on the SQLiteDatabase class, so can be executed on the db object from the question like so:

String[] columns = new String[]{"column1", "column2", "column3"};
Cursor cursor = db.query("TableName", columns, null, null, null, null, null);

Upvotes: 0

Srishti Roy
Srishti Roy

Reputation: 576

This may help

public ArrayList<ToDolistEntity> getAllTodolist(){
        ArrayList<ToDolistEntity> labels = new ArrayList<ToDolistEntity>();
        ToDolistEntity entity;
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_DOLIST;

        SQLiteDatabase db = this.getReadableDatabase();
       Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {

                entity=new ToDolistEntity();
                //labels.add(cursor.getString(1));

                entity.setCheckid(cursor.getInt(1));
                entity.set_toDolisttext(cursor.getString(2));           

                labels.add(entity);


            } while (cursor.moveToNext());

        }

        // closing connection
        cursor.close();
        db.close();

        // returning lables
        return labels;
    }

Upvotes: 0

Andrain
Andrain

Reputation: 910

the single query will not return columns you also have to give the names of columns which you want to retrieve from the database

public ArrayList getDataFromDatabase() 
{
   ArrayList array_list = new ArrayList();
   SQLiteDatabase db = this.getReadableDatabase();
   Cursor res =  db.rawQuery( "give your query here", null );
   res.moveToFirst();
   while(res.isAfterLast() == false)
    {
     //give the name of columns you want to retrieve from database one by one  
     array_list.add(res.getString(res.getColumnIndex(column 1)));
     array_list.add(res.getString(res.getColumnIndex(column 2)));
     res.moveToNext();
}
  return array_list;
}

Upvotes: 3

Related Questions