Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 241
DienTrinh Avatar asked Mar 18 '11 02:03

DienTrinh


People also ask

Where with multiple conditions SQLite?

This SQLite WHERE clause example uses the WHERE clause to define multiple conditions, but instead of using the AND Condition, it uses the OR Condition. In this case, this SELECT statement would return all employee_id, last_name, and first_name values from the employees table where the employee_id is 1 or 2.

How do I query two tables in SQLite?

To query data from multiple tables, you use INNER JOIN clause. The INNER JOIN clause combines columns from correlated tables. Suppose you have two tables: A and B. A has a1, a2, and f columns.

Can SQLite be access by multiple processes?

SQLite allows multiple processes to have the database file open at once, and for multiple processes to read the database at once. When any process wants to write, it must lock the entire database file for the duration of its update. But that normally only takes a few milliseconds.


2 Answers

@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);
like image 136
keno Avatar answered Sep 26 '22 14:09

keno


The solution is:

SELECT * FROM Employess 
WHERE DEPT='Sales' COLLATE NOCASE AND City='HCM' COLLATE NOCASE
like image 28
valkot Avatar answered Sep 26 '22 14:09

valkot