我有一個數據庫中的表,我想只顯示此表的一行。該表有3個字段(ID,標題和說明)。 我想根據標題過濾行。過濾數據庫中的查詢SQLite
我有這樣的代碼:
光標光標= db.query(TABLE_NAME,FROM,NULL,NULL,NULL,NULL,ORDER_BY);
其中第三個字段是選擇之一(一個字符串)。但我不知道我必須準確地選擇我想要顯示的行。由於
我有一個數據庫中的表,我想只顯示此表的一行。該表有3個字段(ID,標題和說明)。 我想根據標題過濾行。過濾數據庫中的查詢SQLite
我有這樣的代碼:
光標光標= db.query(TABLE_NAME,FROM,NULL,NULL,NULL,NULL,ORDER_BY);
其中第三個字段是選擇之一(一個字符串)。但我不知道我必須準確地選擇我想要顯示的行。由於
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
試試這個
Cursor cursor = db.query("TABLE_NAME",new String[]{"ColumnName"}, "ColumnName=?",new String[]{"value"}, null, null, null);
我只想嘗試選擇一行或其中一些,而不是顯示所有的表格。例如,我想顯示標題行:TITLE = test – danizp 2011-05-10 10:47:03
請參閱我的編輯答案.... – 2011-05-10 10:53:37
你可以在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;
}
你想要哪一行? – 2011-05-10 10:37:17