0

我想在整个数据库表中搜索特定字符串并获取匹配列的行详细信息。例如,我有如下表

  **name  email   address  designation**
    sai   xy@mail  75385      nagar
    thiru  y@mail  75893      city

现在我有像“nagar”或“thiru”这样的搜索键,如何为它准备查询。如果有人知道请帮助我。提前致谢。

4

2 回答 2

2

将整个表格读入游标并在每一列中搜索以获取列名。

可能是这样的:

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

DataBaseAdapter 类中的 fetchAllData() 函数包含以下代码:

public Cursor fetchAllData(){

  try {
   return database.query(TABLENAME, null, null, null, null,
     null, null);
  } catch (Exception e) {
   return null;
  }
}

这将给出您的密钥的列名。

所以现在很容易运行这样的查询:

 String query=  "SELECT * FROM tablename where "+getColumnName()+"='"+key+"'" ;
于 2012-08-09T12:25:09.530 回答
1

试试这个查询..

SELECT * FROM tablename WHERE name = 'nagar' or email = 'nagar' or address = 'nagar' or designation = 'nagar';

或者

SELECT * FROM tablename WHERE name or email or address or designation = 'nagar';
于 2012-08-09T12:17:51.367 回答