6

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?

3 Answers 3

11

@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);
2

The solution is:

SELECT * FROM Employess 
WHERE DEPT='Sales' COLLATE NOCASE AND City='HCM' COLLATE NOCASE
1
SELECT * FROM Employees WHERE DEPT='Sales' AND City='HCM'
3
  • it works in SQL but it doesn't work in SQLite. How can I use that command in SQLite with syntax: db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy).
    – DienTrinh
    Mar 18, 2011 at 3:18
  • I tried to use Cursor cursor = db.query(TABLE_NAME, new String [] {_ID, NAME, DEPT, CITY } , DEPT +"=?" +"AND" +CITY +"=?" , new String[] {"Sales", "HCM" }, null, null, null);. But the emulator show an error
    – DienTrinh
    Mar 18, 2011 at 3:34
  • Shouldn't this type of query be ran with the db.rawQuery method rather than db.query? I think that was the problem.
    – Paulina D.
    Feb 18, 2013 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.