0

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.

2
  • 2
    When you use the query method from SQLiteQueryBuilder it has a projection parameter. String: 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. Commented Apr 26, 2016 at 10:37
  • I have heard that SQLiteQueryBuilder can perform JOINs. I will look into it. Commented Apr 26, 2016 at 10:40

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I forgot to mention it involves a JOIN too.
0

You can do it like this-

ArrayList<String> dynamicColumnList = new ArrayList<>();
//add column dynamically to the list  
String columns[]= dynamicColumnList.toArray(new String[0]);
sqLiteDatabase.query("TableName",columns,"id",new String[]{id},null,null,null);

Comments

0

Can use SQLiteQueryBuilder.

Add the JOIN with setTables and pass the String array to projectionIn argument of query

Thanks to @RalphBergmann.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.