SELECT WHERE sql query Android question
I'm a complete newbie to programming and am having problems implementing a SELECT query with a WHERE year_id = 'a selected year variable' to display results in a list view. My code as shown below displays all entries stored in the database. How can I alter it to filter it down to a selected year within year_id as anything I try to do crashes my app? I have spent hours searching for tutorials and similar questions but am still lost as regards an answer.......would really appreciate some help
MyDBHelper mh = new MyDBHelper(this);
mDb = mh.getWritableDatabase();
mCursor = mDb.query("tracker", null, null, null, null, null,
"category_name ASC")开发者_如何学运维;
startManagingCursor(mCursor);
int view[] = {R.id.categoryNameView, R.id.dataView, R.id.placeView, R.id.amountView};
String col[] = { "category_name", "date", "place", "amount"};
SimpleCursorAdapter ca = new SimpleCursorAdapter(
this, R.layout.display_item, mCursor, col, view);
DisplayAllView = (ListView) findViewById(R.id.DisplayDateListView);
DisplayAllView.setAdapter(ca);
Have a look at the javadoc for db.query
3d and 4th arg are
selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table. selectionArgs You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.
So you would do something like
mCursor = mDb.query("tracker", null,
"year_id = ?", new String[] { String.valueOf(year)},
null, null, "category_name ASC");
精彩评论