.rawQuery multiple ? parameter
I am trying to use the .rawQuery to return the contents of a column in an SQLite database. I have a list that is used to query the database and depending on what is clicked depends on what is returned. The problem is that the first item in the list wants to return all results and those below it only certain results.
I am using the following code:
public Cursor getTrailByType(String id) {
String[] args={id};
retu开发者_JS百科rn(getReadableDatabase()
rawQuery("SELECT _id, NAME FROM trail WHERE TYPE_id=? OR ENT=?",
args));
}
TYPE_id is the specific result to return (if ? is between 2 and 10) and ENT is return all (if ? = 1 ; I have simply added a second column ENT with the number 1 in all rows). So, if id is 1 it returns the ENT criteria but if it is between 2 and 10 it returns the TYPE_id criteria.
The problem is the second ? is not read and a blank list is produced. If I substitue the second ? for an integer then it works but does not populate the specific lists properly.
Can anyone suggest what is wrong with the code?
try this
public Cursor getTrailByType(String id, boolean needAll) {
String[] args = null;
String query = "SELECT _id, NAME FROM trail";
if(!needAll){
query += " WHERE TYPE_id=?";
args = new String[]{id};
}
return (getReadableDatabase().rawQuery(query, args));
}
you can get rid of ENT column and try this ...
// put any id and 1 as ent to get all put id and ent=0 to get specific id
public Cursor getTrailByType(String id, int ent) {
String[] args={id, Integer.toString(ent)};
return(getReadableDatabase().
rawQuery("SELECT _id, NAME FROM trail WHERE TYPE_id=? OR 1=?", args));
}
EDIT: heh need to convert int to string
精彩评论