android - SQLite rawQuery from BuildUnionQuery Selecting just the first true condition -
i have following code in android studio
string[] buildquery={ "select anatomia "+usuariosinfodbh.tablausuarioname+" anatomia='si' , usuario='"+nombre+"';", "select bioquimica "+usuariosinfodbh.tablausuarioname+" bioquimica='si' , usuario='"+nombre+"';", "select histologia "+usuariosinfodbh.tablausuarioname+" histologia='si' , usuario='"+nombre+"';", "select fisiologia "+usuariosinfodbh.tablausuarioname+" fisiologia='si' , usuario='"+nombre+"';", "select embriologia "+usuariosinfodbh.tablausuarioname+" embriologia='si' , usuario='"+nombre+"';", "select microbiologia "+usuariosinfodbh.tablausuarioname+" microbiologia='si' , usuario='"+nombre+"';", "select urgencias "+usuariosinfodbh.tablausuarioname+" urgencias='si' , usuario='"+nombre+"';", "select farmacologia "+usuariosinfodbh.tablausuarioname+" farmacologia='si';", "select nutricion "+usuariosinfodbh.tablausuarioname+" nutricion='si' , usuario='"+nombre+"';", "select patologia "+usuariosinfodbh.tablausuarioname+" patologia='si' , usuario='"+nombre+"';", "select fisiopatologia "+usuariosinfodbh.tablausuarioname+" fisiopatologia='si' , usuario='"+nombre+"';" }; sqlitequerybuilder qb=new sqlitequerybuilder(); string query=qb.buildunionquery(buildquery, null, null); cursor c= db.rawquery(query, null);
when run pertinent log.d know how many columns being selected, shows me 1, first column condition met (which anatomia='si' in case). know first 1 becase if remove select... anatomia='si', log.d prints nutricion='si', next condition true. want code select columns condition set fullfilled, not first meets it. thank in advance. ps: (log.d)s i'm using are:
log.d("countcolumsreg", integer.tostring(c.getcount()));//0 log.d("countcolumns", integer.tostring(c.getcolumncount()));//0
and
string[] usuariomaterias= new string[c.getcolumncount()]; (int i=0; i<c.getcolumncount();i++) { usuariomaterias[i] = c.getcolumnname(i); log.d("arrayseg",usuariomaterias[i]); //anatomia }
that's union
does: performs bunch of queries, , merges results each 1 result set. when using union
without all
modifier, duplicate rows removed. why you're getting 1 row -- matching rows contain 'si'
, they're duplicates.
side note: prevent sql injection attacks, should use queries ?
placeholders, supplying arguments in separate value array (the second argument, selectionargs
, in case of rawquery()
).
i'm not sure, seems want know whether there 'si' in each column. here's alternative gives true/false each:
select anatomia='si' anatomia, bioquimica='si' bioquimica, histologia='si' histologia, ... yourtable usuario=?
this give result looks like:
anatomia bioquimica histologia ... 1 0 1 ...
however, maybe should consider changing table design make columns boolean
instead -- because 'si' sounds boolean value. way, can select * from...
or @ least select anatomia, bioquimica, histologia... from...
instead, making code prettier. :)
update after @luise replied comment:
there isn't convenient way treat columns "data" in sql. best option can come (assuming we've made change 'si' actual boolean values):
select 'anatomia' yourtable usuario=?1 , anatomia union select 'bioquimica' yourtable usuario=?1 , bioquimica union select 'histologia' yourtable usuario=?1 , histologia ...
but can see, still requires bunch of similar selects. not pretty.
btw, through this, i'm assuming usuario unique (either primary key
or unique
).
Comments
Post a Comment