SQlite Query in android using cursor

和自甴很熟 提交于 2019-12-11 11:04:24

问题


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.


回答1:


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.




回答2:


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



回答3:


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
}


来源:https://stackoverflow.com/questions/28957644/sqlite-query-in-android-using-cursor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!