DienTrinh
DienTrinh

Reputation: 73

how do I query SQLite database with two conditions?

I have a table about employee with 3 columns like following code:

 db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + _ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + DEPT
                + " TEXT NOT NULL," + NAME + " TEXT NOT NULL," + CITY + " TEXT NOT NULL);");

now I just want to show employees in the same both DEPT and CITY(e.i both employees in HCM City and Sales department). How can I query to get it?

Upvotes: 6

Views: 16751

Answers (3)

valkot
valkot

Reputation: 21

The solution is:

SELECT * FROM Employess 
WHERE DEPT='Sales' COLLATE NOCASE AND City='HCM' COLLATE NOCASE

Upvotes: 2

keno
keno

Reputation: 2966

@DienTrinh, you had it right, note the spaces around AND. That should work for you.

 Cursor cursor = db.query(TABLE_NAME, 
                          new String [] {_ID, NAME, DEPT, CITY }, 
                          DEPT +"=?" +" AND " + CITY +"=?", 
                          new String[] {"Sales", "HCM" }, 
                          null, null, null);

Upvotes: 11

Dmytrii Nagirniak
Dmytrii Nagirniak

Reputation: 24078

SELECT * FROM Employees WHERE DEPT='Sales' AND City='HCM'

Upvotes: 1

Related Questions