Munchoo
Munchoo

Reputation: 313

SQlite Query in android using cursor

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.

Upvotes: 1

Views: 5671

Answers (3)

YFeizi
YFeizi

Reputation: 1528

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.

Upvotes: 5

Vignesh Bala
Vignesh Bala

Reputation: 663

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
}

Upvotes: 1

Omer Sonmez
Omer Sonmez

Reputation: 1168

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"};

Upvotes: 1

Related Questions