简体   繁体   中英

android content provider query builder

I have variables, each of them with the possibility of being empty. I use these to create my arguments:

String[] arguments = {argClassOfExercise, argPeriodOfPeriodisation, argAgeGroup, argPrDistanceGroup, argVo2Max};

and to define my WHERE CLAUSE I use

String selection = "catalog.ex_class = ? AND catalog.period = ? AND catalog.age_group = ? AND catalog.pr_comp_grouping = ? AND catalog.vo2max = ?";

putting it all in my cursorloader:

CursorLoader cursorLoader = new CursorLoader(getActivity(), CatalogProvider.CONTENT_URI_TRANSLATION, projection, selection, arguments, null);

Now this will all work fine until one of the variables is empty. Since none of the database values will be equal to and empty string. Is there a possibility to exclude a WHERE CLAUSE if that particular variable is empty?

I know you can write a bunch of IF statements but that would result in me writing 11 of them and I don't think that's really efficient.

Any ideas are welcome

It would be possible to use an SQL expression like (ex_class = ?1 OR ?1 = '') AND ... , but this would be very inefficient.

You should construct the SQL selection string and the arguments array dynamically, and just leave out any entries for emptry strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM