totoro totoro - 7 months ago 49
Java Question

Android sqlite JOIN query with the select columns given by String[]

I have string array with columns I want to select:

String[] columns = {"col1", "col2", "col3"};


The columns might change in the future, but will always be a subset of available columns.

I cannot find a
SQLiteDatabase
API that lets me pass this array as columns. The only solution I have so far is to just string join the columns with
,
as separator and add it into my query string and perform a
rawQuery
.

Is there a better solution?

EDIT

The query is a
JOIN
query.

Answer

The query() methods in SQLiteDatabase take in a String[] columns argument which seems to be the thing what you are looking for.

Raw queries with string joining with , works, too, and that's basically what query() does under the hood.

Oh, I forgot to mention it involves a JOIN too

Then your best options is probably rawQuery() with SQL you're building yourself. Android SQLite wrapper APIs are really not too flexible.