1

My Query is SELECT DefaultId FROM tblsample where ('567' = DefaultId) || ('567' = Name)

In the above query

table name = tblsample 
Column Names = DefaultId , Name
Input Value  = 567

Now i want to check this value is available in which column from the table and return its DefaultId. I am able to achieve this in Sqlite, want to know is there still better way to optimize the query and In android using

database.query(boolean distinct, String table, String[] columns,String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)

which query i must use in android.

3 Answers 3

5

Use this snippet:

String table = "tblsample";
String selection = "DefaultId =? OR Name=?";
String[] selectionArgs = new String[]{"567"};
String[] projection = new String[]{"DefaultId","Name"}; // if you want to fetch only these two columns 

Before your database.query:

database.query(true, table, projection, selection, selectionArgs, null, null, null, null);

Change the values for distinct, limit, orderBy, having and groupBy if you need them.

Sign up to request clarification or add additional context in comments.

4 Comments

i accept your answer but why i must give my selectionArgs an Array instad its a single value
Usually its an array of values then use it as an array , i dont test it with one value and a single String variable.
whats wrong with my answer @Munchoo ? i cant understand why you didnt accept my answer :/
i tried accepting both answers but i do not know that we can accept only one. sorry changed nw
1

You can use SQLiteDatabase documentation to guide you.

Table

  • table = "tblsample";

Columns

  • columns = new String[]{"DefaultId"};

Where

  • selection = "DefaultId =? OR Name=?";
  • selectionArgs = new String{"567"};

Comments

1
String sql = "SELECT DefaultId FROM tblsample where (DefaultId = 567) OR (Name = 567)";
Cursor cursor=database.rawQuery(sql,null);
if(cursor != null && cursor.getCount()>0){
    //value available
} else{
    //not avail
}

1 Comment

Is it possible to use || in SQLite ?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.