我想搜索整个数据库表中的特定字符串,并获取匹配列的行详细信息。例如,我的表格如下所示
**name email address designation**
sai xy@mail 75385 nagar
thiru y@mail 75893 city
现在我正在使用像“nagar”或“thiru”这样的搜索键,如何为它准备查询。如果有人知道请帮助我。提前谢谢。
答案 0 :(得分: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+"'" ;
答案 1 :(得分: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';