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);
Related
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");
}
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.)
I have a table called tbl_homework. There are 3 columns called "_id", "hw" and "hwdate".
Now I like to read out "hw" AND "hwdate". So I like it on the same line in this listview.
I have to say that it is working without crash or error. It just don't show the date...
Following picture will tell you what I mean:
Here are the code snippets to tell you how I tried this:
private Button.OnClickListener add_hw = new Button.OnClickListener(){
public void onClick(View arg0) {
mDbHelper.open_database_rw();
String txt_insert_hw = insert_hw.getText().toString();
if(txt_insert_hw.equals("")){
doMessage("Keine Eingabe!");
}else{
String date_picker_message= date_pick.getDayOfMonth() + "/" + (date_pick.getMonth()+1) + "/" + date_pick.getYear();
final String INSERT_HW = "INSERT INTO tbl_homework ('hw', 'hwdate') VALUES ('"+txt_insert_hw+"', '"+date_picker_message+"')";
db.execSQL(INSERT_HW);
insert_hw.setText("");
fillData();
}
}
};
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = mDbHelper.fetchAllNotes();
startManagingCursor(c);
String[] from = new String[] { dbHelper.KEY_TITLE, dbHelper.KEY_DATE};
int[] to = new int[] {R.id.txt_notes_row};
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
And in mDbHelper:
public Cursor fetchAllNotes() {
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_DATE}, null, null, null, null, null);
}
Soulution of this problem: (Received from Jury)
String[] from = new String[] { dbHelper.KEY_TITLE, dbHelper.KEY_DATE};
int[] to = new int[] {R.id.txt_notes_row, R.id.txt_notes_row2};
I had to add a new TextView. dbHelper.KEY_TITLE AND dbHelper.KEY_DATE coudn't be handle by one TextView. So I had to add a new TextView to handle the second part of my String "from".
I think the problem is that you try to match two columns (from) to only one container (to) here:
String[] from = new String[] { dbHelper.KEY_TITLE, dbHelper.KEY_DATE};
int[] to = new int[] {R.id.txt_notes_row};
You should specify two fields in the list row where you store your values. I.e. your R.id.txt_notes_row should contain two widgets (for instance, R.id.txtview1 and R.id.txtview2) where you can store your values from db.
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.
I'm doing a project for my Android class. The app is very ugly and not very useful, but it's to demonstrate that we can implement Content Providers. The problem I'm having is sorting the Cursor. Here is my code:
setContentView(R.layout.main);
String[] projection = new String[] {Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER};
Cursor mCursor = this.getContentResolver().query(Phone.CONTENT_URI, projection , null, null, "Phone.CONTACT_ID ASC");
startManagingCursor(mCursor);
ListAdapter adapter = new SimpleCursorAdapter(
this, // Context.
R.layout.rows,
mCursor,
new String[] {Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER},
new int[] {R.id.text1, R.id.text2, R.id.text3});
setListAdapter(adapter);
Instead of doing this:
..., "Phone.CONTACT_ID ASC");
try this
..., Phone.CONTACT_ID + " ASC");