porthfind
porthfind

Reputation: 1619

Get values from a database table by concatening 2 columns

I'd like to do this query in SQLite:

selectQuery = "SELECT " + col_MES + " || " + col_ANO + " AS mes FROM " + TABLE_DESPESA + ";";

and I'm using the following code to catch the values:

        cursor = db.rawQuery(selectQuery, null);

        if (cursor != null) {
            while (cursor.moveToNext()) {

                meses.add(cursor.getString(cursor.getColumnIndex(col_MES)));
            }
        }

where meses is a List<String>.

But I don't know what should I put on

(cursor.getString(cursor.getColumnIndex(col_MES))) 

because on the select I'm concatening 2 columns (col_mes and col_ano), so it's not right the getColumnIndex(col_mes), but what should I write?

Upvotes: 1

Views: 44

Answers (1)

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

You need to use the alias of the combined columns.
In your case,

meses.add(cursor.getString(cursor.getColumnIndex("mes")));

As defined in your query:

selectQuery = "SELECT " + col_MES + " || " + col_ANO + " AS mes FROM " + ...

AS mes defines "mes" as the alias of your combined field.

Upvotes: 2

Related Questions