Read the whole table into a cursor and search in each column to get column name.
May be like this:
private String getColumnName(String key){
String columnName="";
DataBaseAdapter dba=new DatabaseAdapter(getApplicationContext());
dba.open();
Cursor cr=dba.fetchAllData();
cr.moveToFirst();
while(!cr.isAfterLast()){
if(key.equals(cr.getString(cr.getColumnIndex("name")))){
columnName="name";
}
cr.moveToNext();
}
cr.moveToFirst();
while(!cr.isAfterLast()){
if(key.equals(cr.getString(cr.getColumnIndex("email")))){
columnName="email";
}
cr.moveToNext();
}
cr.moveToFirst();
while(!cr.isAfterLast()){
if(key.equals(cr.getString(cr.getColumnIndex("address")))){
columnName="address";
}
cr.moveToNext();
}
cr.moveToFirst();
while(!cr.isAfterLast()){
if(key.equals(cr.getString(cr.getColumnIndex("designation")))){
columnName="designation";
}
cr.moveToNext();
}
return columnName;
}
fetchAllData() function in DataBaseAdapter class contains following code:
public Cursor fetchAllData(){
try {
return database.query(TABLENAME, null, null, null, null,
null, null);
} catch (Exception e) {
return null;
}
}
This will give column name of your key.
So now it's easy to run query like this:
String query= "SELECT * FROM tablename where "+getColumnName()+"='"+key+"'" ;