user1880779
user1880779

Reputation: 2078

Android SQLite Query Where and Where

I need to return the ID value from the database where dan == table_column_one and where vrijeme == table_column_two. I don't know how to do this.

public static int returnID(String dan, int vrijeme){
        Cursor cursor;
        int IDRQ;
        cursor = db.query
                (
                        TABLE_NAME,
                        new String[] { TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO },                       
                        new String[] {TABLE_COLUMN_ONE + " like " + "'%" + dan + "%'", TABLE_COLUMN_TWO + " like " + "'%" + vrijeme + "%'"}, null, null, null, null
                );  
        cursor.moveToFirst();           
        IDRQ = cursor.getInt(0);
        return IDRQ;
    }   );  

This is how I tried, but of course, its wrong.

Upvotes: 2

Views: 11411

Answers (1)

alex
alex

Reputation: 6409

In SQLiteDatabase.query() the selection comes in two parts. The where clause (a String) and the whereArgs (an array of String).

To add more than one condition to the where clause you can use AND or OR, just like && or || in Java.

A question mark in the where clause is bound to one of the Strings in the whereArgs array.

cursor = db.query(TABLE_NAME, new String[] { TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO },
TABLE_COLUMN_ONE + " LIKE ? AND " + TABLE_COLUMN_TWO + " LIKE ?",                           
new String[] {"%" + dan + "%", "%" + vrijeme + "%"},
null, null, null, null);

Upvotes: 9

Related Questions