2011-05-10 120 views
2

我有一个数据库中的表,我想只显示此表的一行。该表有3个字段(ID,标题和说明)。 我想根据标题过滤行。过滤数据库中的查询SQLite

我有这样的代码:

光标光标= db.query(TABLE_NAME,FROM,NULL,NULL,NULL,NULL,ORDER_BY);

其中第三个字段是选择之一(一个字符串)。但我不知道我必须准确地选择我想要显示的行。由于

+0

你想要哪一行? – 2011-05-10 10:37:17

回答

6
String[] FROM = { // ID of the column(s) you want to get in the cursor 
     ID, 
     Title, 
     Description 
}; 

String where = "Title=?"; // the condition for the row(s) you want returned. 

String[] whereArgs = new String[] { // The value of the column specified above for the rows to be included in the response 
     "0" 
    }; 

return db.query(TABLE_NAME, FROM, where, whereArgs, null, null, null); 

这应该给你一个光标,你的所有列,但只包含行,其中的标题列的值等于0

+0

谢谢它的作品! – danizp 2011-05-10 10:55:05

+0

@ user736605不要忘记将其标记为已接受的答案。 :)点击旁边的复选标记。 – Klaus 2011-05-10 11:17:53

+0

@TofferJ,这里的标题或表格是“表格列的值等于0”的意思! – 2013-10-10 02:19:58

1

试试这个

Cursor cursor = db.query("TABLE_NAME",new String[]{"ColumnName"}, "ColumnName=?",new String[]{"value"}, null, null, null); 
+0

我只想尝试选择一行或其中一些,而不是显示所有的表格。例如,我想显示标题行:TITLE = test – danizp 2011-05-10 10:47:03

+0

请参阅我的编辑答案.... – 2011-05-10 10:53:37

0

你可以在SQLite中按以下代码搜索;

In MainActivity;

search.addTextChangedListener(new TextWatcher() { 
    public void afterTextChanged(Editable s) { 
    } 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    } 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     adapter.getFilter().filter(s.toString()); 
    } 
}); 
adapter.setFilterQueryProvider(new FilterQueryProvider() { 
    public Cursor runQuery(CharSequence constraint) { 
     return 
//Here you can filter data by any row , just change text replace of "subject" 
dbManager.fetchdatabyfilter(constraint.toString(),"subject"); 
    } 
}); 

DatabaseHelper.java

public Cursor fetchdatabyfilter(String inputText,String filtercolumn) throws SQLException { 
Cursor row = null; 
String query = "SELECT * FROM "+DatabaseHelper.TABLE_NAME; 
if (inputText == null || inputText.length() == 0) { 
    row = database.rawQuery(query, null); 
}else { 
    query = "SELECT * FROM "+DatabaseHelper.TABLE_NAME+" WHERE "+filtercolumn+" like '%"+inputText+"%'"; 
    row = database.rawQuery(query, null); 
} 
if (row != null) { 
    row.moveToFirst(); 
} 
return row; 
}