开发者

how do I query SQLite database with two conditions?

开发者 https://www.devze.com 2023-02-17 18:58 出处:网络
I have a table about employee with 3 columns like following code: db.execSQL(\"CREATE TABLE \" + TABLE_NAME + \" (\" + _ID

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 em开发者_运维问答ployees in the same both DEPT and CITY(e.i both employees in HCM City and Sales department). How can I query to get it?


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


The solution is:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消