Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android sqlite query with multiple where

Tags:

android

sqlite

Here's my current sqlite code:

Cursor c = sqlDatabase.rawQuery("select docid as _id, recipeID from " + TABLE_RECIPE_NAME + 
                " where " + KEY_ownerID + " = ? ", new String[] { ownerID});

It works fine, but when I tried adding multiple where to something like this:

Cursor c = sqlDatabase.rawQuery("select docid as _id, recipeID from " + TABLE_RECIPE_NAME + 
                " where " + KEY_ownerID + " = ?, " + KEY_partnerID + " = ?, " + KEY_advertiserID + " = ?, " + KEY_chefID + " = ?", new String[] { ownerID, partnerID, advertiserID, chefID });

It returns an error. So how do I deal with multiple ?

like image 689
imin Avatar asked Jan 30 '12 08:01

imin


People also ask

How to where clause in Android SQLite?

This example demonstrate about How to where Clause in Android sqlite. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

What is the use of SQLite in Android?

SQLite is an open source SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation. SQLite supports all the relational database features. In order to access this database, you don't need to establish any kind of connections for it like JDBC, ODBC etc.

Can I write my own SQLite queries?

This was an introduction to writing SQLite queries and the basics of querying the database and how you can filter the returned data. You can now, write your own SQLite queries.

How to group multiple rows into one value in SQLite?

This will give you only three unique values for the department name column: SQLite Aggregates are built-in functions defined in SQLite that will group multiple values of multiple rows into one value. Here are the aggregates supported by SQLite: Returned the average for all the x values.


2 Answers

change query to:

Cursor c = sqlDatabase.rawQuery("select docid as _id, recipeID from " +
TABLE_RECIPE_NAME + " where " + KEY_ownerID + " = ? AND " + KEY_partnerID +
" = ? AND  " + KEY_advertiserID + " = ? AND " + KEY_chefID + " = ?",
new String[] { ownerID, partnerID, advertiserID, chefID });
like image 148
jeet Avatar answered Oct 05 '22 08:10

jeet


You are doing correct but just need to use AND or OR keyword between each where condition as per your requirement. So try using this once.

like image 43
Kailas Bhakade Avatar answered Oct 05 '22 10:10

Kailas Bhakade