Android: Select Query SQLite for ListView - android

I'm using this method to display a table and its content from SQLite on a ListView:
listView.setAdapter(new MySimpleCursorAdapter(this, R.layout.listitems,
managedQuery(Uri.withAppendedPath(Provider.CONTENT_URI,
Database.Project.NAME), new String[] { BaseColumns._ID,
Database.Project.C_PROJECTTITLE,
Database.Project.C_SMALLIMAGE, Database.Project.C_PROJECTDESCRIPTION, Database.Project.C_ORGANIZATIONTITLE, Database.Project.C_DONATIONAMOUNT, Database.Project.C_KEYWORD, Database.Project.C_PRICE, Database.Project.C_SHORTCODE}, null, null, null),
new String[] { Database.Project.C_PROJECTTITLE,
Database.Project.C_SMALLIMAGE, Database.Project.C_PROJECTDESCRIPTION, Database.Project.C_ORGANIZATIONTITLE, Database.Project.C_DONATIONAMOUNT}, new int[] {
R.id.txt_title, R.id.image, R.id.txt_list_desc, R.id.txt_org, R.id.btn_amount}));
Now I'm wondering how am I able to do this:
For example I want to display only Database.Project.C_PROJECTTITLE which contains the word 'Health' in its column.
So only the PROJECTS whose PROJECTTITLE contain the word 'Health' will show up on the list.
It doesn't have to be EQUAL 'Health', so the PROJECTTITLE with the name for example 'Health/Bio' also appears on the list. How am I able to perform that?

You query should be something like below:
db.query(TABLE_NAME, new String[] {"_id", "yourColumn"},"yourColumn like " + "'%Health%'", null, null, null, null);

If you let the search term be %health% then it should select all rows that contain health.

Related

How to get all the items in sql using query based on a WHERE clause

I am new to database and I am trying to get all the data from table whose category column is equal to 1. This is my first attempt. Can someone please guide me where I am doing wrong?
public Cursor getAllFamilyMovies()
{
return database.query("movies", new String[] {"_id", "name"},
"category=1", null, null, null, "name");
}
The "whereclause" and "whereargs" need to be separated as shown below:
public Cursor getAllFamilyMovies() {
return database.query("movies", new String[] {"_id", "name"}, "category = ?", new String[] {"1"}, null, null, "name");
}

Fetch specific data from android database sqlite

I'm wondering if it is possible to fetch one specific type of data from an android database, based on sqlite.
Let's say I have a table with rows "Category" and "Title" and I want to get a String array containing the "Title" of those categories matching a given one.
For example:
Title Category
A spam
B important
C spam
And given "spam", I want to get a String array like
S = {A,C}
Is there a way to do this?
Please note that I'm very new to databases.
Thanks in advance.
EDIT:
I'm actually trying with a query
mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
KEY_BODY, KEY_CATEGORY}, KEY_CATEGORY + "=" + category, null, null, null, KEY_CATEGORY);
But it returns a Cursor and I need a SimpleCursorAdapter like here for formatting
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
mList.setAdapter(notes);
where from and to are:
String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.text1 };
FINAL EDIT:
It finally worked, with this code provided by #ELITE
Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY, KEY_CATEGORY}, KEY_CATEGORY + "=?", new String[]{category}, null, null, KEY_CATEGORY);
ArrayList elements = new ArrayList();
while(cursor.moveToNext()) {
elements.add(cursor.getString(cursor.getColumnIndex(KEY_TITLE)));
}
// use elements variable,
// here you'll get ["A", "C"]
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, cursor, from, to);
mList.setAdapter(notes);
Iterator over the cursor and store the result in ArrayList or Vector and then use it.
Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY, KEY_CATEGORY}, KEY_CATEGORY + "=?", new String[]{category}, null, null, KEY_CATEGORY);
ArrayList elements = new ArrayList();
while(cursor.moveToNext()) {
elements.add(cursor.getString(cursor.getColumnIndex(KEY_TITLE)));
}
cursor.close();
// use elements variable,
// here you'll get ["A", "C"]
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, elements, from, to);
mList.setAdapter(notes);
Hope it'll work.
For more details refer this question
Use the query() of SQLiteDatabase class like this:
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("<name of the table>", new String[]{}, "Category=?", new String[]{"spam"}, null, null, null);

SimpleCursorAdapter returns empty data

When I am trying to put data from Database to ListView via SimpleCursorAdapter, the rows of the ListView are shown but they are empty.
Like:
[___________]
[___________]
[___________]
Instead of:
[TEXT]
[TEXT]
[TEXT]
My code:
db = provider.getReadableDatabase();
String[] columns = {"_id", "Title"};
Cursor cursor = db.query(NotificationsProvider.TABLE_NAME, columns, null, null, null, null, null);
Log.d(TAG, "COUNT: "+cursor.getCount());
String[] clm = {"Title"};
int[] to = {android.R.id.list};
cursorAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
cursor, clm, to, 0);
setListAdapter(cursorAdapter);
Thanks!
You are simply using the wrong id, try:
int[] to = {android.R.id.text1};
(You can check out simple_list_item_1.xml yourself to verify the appropriate id.)

How to use limit and offset to page through the data

I am new to SQLite and I don't know how to use limit and offset to select a limit number of data from the database, I mean I know the query phrase, but how to use it in a cursor so I can get those data into a listview?
Currently I am using the code below to query data from the database and show them in a listview but it seems I query too much data for one query and the SQLite fail to grow, so I want to split the query into some smaller ones and do it in one time,someone suggested me to try limit and offset,but I googled it there are really not much about it on the Internet.
Would somebody kindly provide me the guide to this? an example or a tutoral, anything will do,thx
channellist = (ListView) findViewById(R.id.Channel);
mDB = new ChannelDB(this);
String[] columns = {mDB.KEY_ID, mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_PATH, mDB.KEY_DBLINK};
String table = mDB.channelS_TABLE;
c = mDB.getHandle().query(table, columns, null, null, null, null, null);
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.channelview,
c,
new String[] {mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_DBLINK},
new int[] {R.id.poster, R.id.channel, R.id.douban});
adapter.setViewBinder(new ChannelViewBinder(this));
channellist.setAdapter(adapter);
pass the last argument with number as string like you need fetch 10 then you can do like this way
c = mDB.getHandle().query(table, columns, null, null, null, null, null,"10");
for more reference see How to use the LIMIT argument in an SQLite Query with Android

Database to ListView - Filter out entries

I have a SQLite database and I'm getting a ListView from the database through a SimpleCursorAdapter. This is the existing code:
Cursor c = mDatabase.query(
"database",
bla = new String[]{
"_id",
"title",
"message",
"time",
"date",
"done"
},
null, null, null, null, null);
startManagingCursor(c);
SimpleCursorAdapter listAdapter =
new SimpleCursorAdapter(this,
R.layout.list_item, c,
new String[]{"title", "message", "time", "date"},
new int[]{R.id.txtv_title, R.id.txtv_message, R.id.txtv_time, R.id.txtv_date}
);
setListAdapter(listAdapter);
This works as intended and I'm getting my ListView.
Now I want that if the "done" field in my database contains the string "false" (instead of "true" ...), this row doesn't get into the ListView.
How can I do this?
Thanks in advance!
This is more a SQL then Android-related. What you're looking for is a Where-clause:
String query = "SELECT _id, title, message, time, date, done "+
"FROM database "+
"WHERE done = 'true' "+
"ORDER BY date";
To send this Query to your SQLite Database, you can use the rawQuery()-method:
final Cursor c = db.rawQuery(query, null);

Categories

Resources