2011-05-10 123 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; 
}