Reputation: 405
i want to write query in sqlite for android in which i want to get a value based upon two values of that row.How can i do that? i have the following query to get value based on one value:
cursor = db.query(TABLE_NAME,new String[] {NAME}, ROLL_NO + " like" + "'%"
+ roll + "%'", null, null, null, null);
Upvotes: 1
Views: 15971
Reputation: 1384
Its Simple try this actid,cusid is string and FF_CUSTOMER_ID,FF_ACTIVITY_ID is column name
Cursor cursor = db.rawQuery("select * from " + TABLE_PROJECT_ACTIVITY_CHECKLIST +" where " + FF_ACTIVITY_ID + " = ? AND " + FF_CUSTOMER_ID + " = ? " , new String[] { actid,cusid});
Upvotes: 0
Reputation: 10462
try this
cursor = db.query(TABLE_NAME,new String[] {NAME}, ROLL_NO + " like" + "'%" + roll + "%' **OR** ", null, null, null, null)
add your second clause after OR in above query
Upvotes: 2
Reputation: 28418
BTW. Guys, have you thought about DB security (SQL injection attacks)? If roll
is smth that user inputs or you get it from a potentially "dangerous" source (file or network), then I'd recommend to rewrite the code to the following:
db.query(
TABLE_NAME,
new String[] {NAME},
ROLL_NO + " like " + "'%?%'",
new String[] { String.valueOf(roll) },
null, null, null
);
Doing this way the DB will "preprocess" the roll
value before executing the query to ensure the value is safe to use. The safe value will be then inserted instead of the ?
char in the ROLL_NO + " like " + "'%?%'"
statement.
Upvotes: 1
Reputation: 89
Thanks. The following code worked for me:
public Cursor fetchMyValues() {
String DATABASE_TABLE="mytablename";
String depthVar="'50'";
String tempVar="'2.5'";
return mDb.query(DATABASE_TABLE, new String[] {"_id", "depth", "temp", "health"},
"temp like " + tempVar + " AND " + "depth like" + depthVar, null, null, null, null);}
This would represent my sql: select health from mytablename where temp='2.5' and depth='50';
hmmm.... I suppose I could do this cleaner with int and/or double, using = instead of like.
Upvotes: 0