Android: Add column to already populated Cursor - android

I'm confused about whether to use MergeCursor or CursorJoiner.
I have a Cursor (A) with a load of data in it. Lets say there are 100 rows in Cursor (A) and 3 columns. What I want to do is insert (append) a new column to the Cursor so the resulting Cursor (B) has 100 rows but 4 columns.
At this moment in time I would like the 4th column to contain a default value for the 100 rows.
How would I do this?

You can use the Decorator pattern here.
For this, Android has CursorWrapper , which is a...
Wrapper class for Cursor that delegates all calls to the actual cursor object. The primary use for this class is to extend a cursor while overriding only a subset of its methods.
Suppose your new column is called newColumn, and that it is of type String then you can do something along these lines:
class MyCursorWrapper extends CursorWrapper {
private final String NEW_COLUMN = "newColumn";
#Override
public int getColumnCount() {
// Count the virtual column in
return getWrappedCursor().getColumnCount() + 1;
}
#Override
public int getColumnIndex(String columnName) {
// Return the virtual column if they are asking for it,
// otherwise just use the original
if (columnName != null && columnName.equals("newColumn") {
return getWrappedCursor().getColumnCount();
}
return mCursor.getColumnIndex(columnName);
}
public int getColumnIndexOrThrow(String columnName)
throws IllegalArgumentException {
// Same logic as getColumnIndex()
if (columnName != null && columnName.equals(NEW_COLUMN) {
return getWrappedCursor().getColumnCount();
}
return getWrappedCursor.getColumnIndexOrThrow(columnName);
}
#Override
public String getColumnName(int columnIndex) {
if (columnIndex == getWrappedCursor.getColumnCount()) {
return NEW_COLUMN;
}
return getWrappedCursor().getColumnName(columnIndex);
}
#Override
public String[] getColumnNames() {
// Add our virtual column to the result from the original Cursor
String original = getWrappedCursor().getColumnNames()
String result = new String[original.length + 1];
System.arrayCopy(original, 0, result, 0, original.length);
result[original.length] = NEW_COLUMN;
return result;
}
#Override
public String getString(int columnIndex) {
// For the last column, return whatever you need to return here
// For real columns, just delegate to the original Cursor
if (columnIndex == getWrappedCursor().getColumnCount()) {
return yourResultHere();
}
return getWrappedCursor().getString(columnIndex);
}
}

Related

Android Sqlite query returning Empty set when there is Data

I am having a Database Adapter that extends SQLiteOpenHelper.
public class DatabaseHelper extends SQLiteOpenHelper {
public SQLiteDatabase database;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
database = this.getWritableDatabase();
}
#Override
public void onCreate(SQLiteDatabase db) {
// Creating Image table
db.execSQL("CREATE TABLE "+IMAGE_TABLE+"("
+IMAGE_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
+IMAGE_DATA+" BLOB NOT NULL);");
db.execSQL("CREATE TABLE "+CREDIT_CATEGORY_TABLE+"("
+CREDIT_CATEGORY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
+CREDIT_CATEGORY_NAME+" VARCHAR(256) UNIQUE NOT NULL,"
+CREDIT_CATEGORY_IMAGE+" INTEGER DEFAULT NULL REFERENCES "+IMAGE_TABLE+"("+IMAGE_ID+") ON DELETE RESTRICT ON UPDATE CASCADE);");
}
}
This is the Database Structure that is relevent to this question. And with such Structure, I am querying a row for populating the RecyclerView Adapter as shown below....
Cursor c = database.query(CREDIT_CATEGORY_TABLE,null,null,null,null,null,null,position+",1");
With the CREDIT_CATEGORY_ID returned from this query, I am allowing the data to be deleted in the application through a method(this method is within DatabaseAdapter).
public boolean deleteCreditCategory(int creditCategoryId) {
if(isCreditCategoryDeletable(creditCategoryId)) {
// TODO DEBUG this: The query function is always returning an Empty set
Cursor c = database.query(CREDIT_CATEGORY_TABLE,new String[] {CREDIT_CATEGORY_IMAGE,CREDIT_CATEGORY_ID},CREDIT_CATEGORY_ID+"=?",new String[] {creditCategoryId+""},null,null,null);
database.beginTransaction();
if( database.delete(CREDIT_CATEGORY_TABLE,CREDIT_CATEGORY_ID+" = ?",new String[] {creditCategoryId+""}) == 1 )
{
// TODO BUG_INFO: since the query method returning empty set, we can't moveToFirst()...
if(!c.moveToFirst()) {
database.endTransaction();
return false;
}
if(c.isNull(c.getColumnIndex(CREDIT_CATEGORY_IMAGE))) {
database.setTransactionSuccessful();
database.endTransaction();
return true;
}
else {
int imageId = c.getInt(c.getColumnIndex(CREDIT_CATEGORY_IMAGE));
if( this.deleteImage(imageId) ) {
database.setTransactionSuccessful();
database.endTransaction();
return true;
}
else {
database.endTransaction();
return false;
}
}
}
else {
database.endTransaction();
return false;
}
}
else
return false;
}
Since this query c.query() is always returning an Empty set, The method c.moveToFirst() is returning false and the DeleteCreditCategory method is returning false indicating a Failure.
I am pretty sure of the results is Empty as I checked that through Debug and Log.d().There is sure data available in the Database.
What have I done wrong here?
I think the issue may be that you delete the row and this is then reflected in the cursor which you then look at and hence is empty, as beleive that a cursor is only actually obtained when you move within it (which includes using the getCount method, so getCount could be used instead of moveToFirst).
I suggest you try to get the row information before the delete and then delete after getting the information from the row.
No use checking for null cursor, it will not be null if the query works.
So perhaps you want something like :-
public boolean deleteCreditCategory(int creditCategoryId) {
if(isCreditCategoryDeletable(creditCategoryId)) {
// TODO DEBUG this: The query function is always returning an Empty set
Cursor c = database.query(CREDIT_CATEGORY_TABLE,new String[] {CREDIT_CATEGORY_IMAGE,CREDIT_CATEGORY_ID},CREDIT_CATEGORY_ID+"=?",new String[] {creditCategoryId+""},null,null,null);
if (Cursor.moveTofirst) {
int imageid = c.getInt(c.getColumnIndex(CREDIT_CATEGORY_IMAGE));
database.beginTransaction();
if (database.delete(CREDIT_CATEGORY_TABLE,CREDIT_CATEGORY_ID+" = ?",new String[] {creditCategoryId+""}) == 1) {
if( this.deleteImage(imageId) ) {
database.setTransactionSuccessful();
database.endTransaction();
return true;
}
}
database.endTransaction();
}
}
return false;
}
Note! I haven't tested this so there may be the odd typo or error.

I need help for getting SQLite data

I use this method for retrieving my data
public String getdata() {
String[] columns= new String[]{RowId,RowBusinessName};
Cursor c=OurDatabase.query(TableName,columns,null,null,null,null,null);
String Result="";
int iRowId=c.getColumnIndex(RowId);
int iRowBusinessName=c.getColumnIndex(RowBusinessName);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
Result=Result+c.getString(iRowBusinessName)+"\n";
}
return Result;
}
How can I make it return structured data (id & business_name)?
I want to display every business_name in a single textview.
Please help
If I understand what you are trying to do, here is the solution if you want to get only 1 RowBusinessName returned as a String. (Hoping that your RowBusinessName is type String).
public String getdata(int rowId) {
String[] columns= new String[]{RowId,RowBusinessName};
Cursor cursor = db.query(TABLENAME, columns, RowId + "=?", new String[]{rowId + ""}, null, null, null, null);
String Result="";
if (cursor != null && cursor.moveToFirst()) {
// not required though
int rowId = cursor.getInt(cursor.getColumnIndexOrThrow(RowId));
String rowBusinessName = cursor.getString(cursor.getColumnIndexOrThrow(RowBusinessName));
result = rowBusinessName;
}
return result;
}
Now if you want a list of RowBusinessName, then you have to build a List<String> rather than appending it to Result. That's not really a good way!
public List<String> getAll() {
List<String> businessNameList = new ArrayList<String>();
String[] columns= new String[]{RowId,RowBusinessName};
Cursor c=OurDatabase.query(TableName,columns,null,null,null,null,null);
if (c != null && c.moveToFirst()) {
// loop until the end of Cursor and add each entry to Ticks ArrayList.
do {
String businessName = cursor.getString(cursor.getColumnIndexOrThrow(RowBusinessName));
if (businessName != null) {
businessNameList.add(businessName);
}
} while (c.moveToNext());
}
return businessNameList;
}
These are work around.
The appropriate answer would be to create an Object that holds id and businessName. That way, you build an object from DB and just return the entire Object.

Closing cursors across JAR boundaries

I use a content provider/resolver, have a separate project/lib that provides a number of DB helper methods. I have a second project/lib that does handy things with a cursor.
Imagine as such DB Helper Method (com.example.DBHelper):
public String[] dumpColumnTable() {
Cursor cursor = cr.query(MY_URI,
new String[] { FIELD },
null,
null,
null
);
return UtilMethods.createArrayFromCursor(cursor);
}
Then the Util methods (com.example.UtilMethods):
public static String[] createArrayFromCursor(Cursor cursor) {
return createArrayFromCursor(cursor, 0);
}
public static String[] createArrayFromCursor(Cursor cursor, int column) {
if (cursor == null) return null;
String[] strings = new String[cursor.getCount()];
if (cursor.moveToFirst()) {
int i=0;
do {
strings[i] = cursor.getString(column);
i++;
} while (cursor.moveToNext());
}
return strings;
}
Obviously the cursor isn't closed. This will leak a cursor. Logcat will give you that message.
SO, close it in the inner util function:
public static String[] createArrayFromCursor(Cursor cursor, int column) {
if (cursor == null) return null;
String[] strings = new String[cursor.getCount()];
if (cursor.moveToFirst()) {
int i=0;
do {
strings[i] = cursor.getString(column);
i++;
} while (cursor.moveToNext());
}
cursor.close();
return strings;
}
But logcat will still claim the cursor wasn't closed before finalize.
If instead, in the DB Helper method, I save the return value, close the cursor, then return it, I get no cursor leak/logcat message:
public String[] dumpColumnTable() {
Cursor cursor = cr.query(MY_URI,
new String[] { FIELD },
null,
null,
null
);
String[] toret = UtilMethods.createArrayFromCursor(cursor);
cursor.close();
return toret;
}
Why ? In debugging, the cursor is marked as close when the calls return. The call stack goes from my activity->db helper->util methods. The db helper and util methods are in separate projects from the activity.
Is there some pass by reference/value issue I'm missing, or crossing multiple JAR boundaries, or the casting of what is a SQLiteCursor to the generic Cursor type that I'm missing ?

What's the best way to iterate an Android Cursor?

I frequently see code which involves iterating over the result of a database query, doing something with each row, and then moving on to the next row. Typical examples are as follows.
Cursor cursor = db.rawQuery(...);
cursor.moveToFirst();
while (cursor.isAfterLast() == false)
{
...
cursor.moveToNext();
}
Cursor cursor = db.rawQuery(...);
for (boolean hasItem = cursor.moveToFirst();
hasItem;
hasItem = cursor.moveToNext()) {
...
}
Cursor cursor = db.rawQuery(...);
if (cursor.moveToFirst()) {
do {
...
} while (cursor.moveToNext());
}
These all seem excessively long-winded to me, each with multiple calls to Cursor methods. Surely there must be a neater way?
The simplest way is this:
while (cursor.moveToNext()) {
...
}
The cursor starts before the first result row, so on the first iteration this moves to the first result if it exists. If the cursor is empty, or the last row has already been processed, then the loop exits neatly.
Of course, don't forget to close the cursor once you're done with it, preferably in a finally clause.
Cursor cursor = db.rawQuery(...);
try {
while (cursor.moveToNext()) {
...
}
} finally {
cursor.close();
}
If you target API 19+, you can use try-with-resources.
try (Cursor cursor = db.rawQuery(...)) {
while (cursor.moveToNext()) {
...
}
}
The best looking way I've found to go through a cursor is the following:
Cursor cursor;
... //fill the cursor here
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
// do what you need with the cursor here
}
Don't forget to close the cursor afterwards
EDIT: The given solution is great if you ever need to iterate a cursor that you are not responsible of. A good example would be, if you are taking a cursor as argument in a method, and you need to scan the cursor for a given value, without having to worry about the cursor's current position.
I'd just like to point out a third alternative which also works if the cursor is not at the start position:
if (cursor.moveToFirst()) {
do {
// do what you need with the cursor here
} while (cursor.moveToNext());
}
Below could be the better way:
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
//your code to implement
cursor.moveToNext();
}
}
cursor.close();
The above code would insure that it would go through entire iteration and won't escape first and last iteration.
How about using foreach loop:
Cursor cursor;
for (Cursor c : CursorUtils.iterate(cursor)) {
//c.doSth()
}
However my version of CursorUtils should be less ugly, but it automatically closes the cursor:
public class CursorUtils {
public static Iterable<Cursor> iterate(Cursor cursor) {
return new IterableWithObject<Cursor>(cursor) {
#Override
public Iterator<Cursor> iterator() {
return new IteratorWithObject<Cursor>(t) {
#Override
public boolean hasNext() {
t.moveToNext();
if (t.isAfterLast()) {
t.close();
return false;
}
return true;
}
#Override
public Cursor next() {
return t;
}
#Override
public void remove() {
throw new UnsupportedOperationException("CursorUtils : remove : ");
}
#Override
protected void onCreate() {
t.moveToPosition(-1);
}
};
}
};
}
private static abstract class IteratorWithObject<T> implements Iterator<T> {
protected T t;
public IteratorWithObject(T t) {
this.t = t;
this.onCreate();
}
protected abstract void onCreate();
}
private static abstract class IterableWithObject<T> implements Iterable<T> {
protected T t;
public IterableWithObject(T t) {
this.t = t;
}
}
}
import java.util.Iterator;
import android.database.Cursor;
public class IterableCursor implements Iterable<Cursor>, Iterator<Cursor> {
Cursor cursor;
int toVisit;
public IterableCursor(Cursor cursor) {
this.cursor = cursor;
toVisit = cursor.getCount();
}
public Iterator<Cursor> iterator() {
cursor.moveToPosition(-1);
return this;
}
public boolean hasNext() {
return toVisit>0;
}
public Cursor next() {
// if (!hasNext()) {
// throw new NoSuchElementException();
// }
cursor.moveToNext();
toVisit--;
return cursor;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
Example code:
static void listAllPhones(Context context) {
Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
for (Cursor phone : new IterableCursor(phones)) {
String name = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.d("name=" + name + " phoneNumber=" + phoneNumber);
}
phones.close();
}
The Do/While solution is more elegant, but if you do use just the While solution posted above, without the moveToPosition(-1) you will miss the first element (at least on the Contact query).
I suggest:
if (cursor.getCount() > 0) {
cursor.moveToPosition(-1);
while (cursor.moveToNext()) {
<do stuff>
}
}
The cursor is the Interface that represents a 2-dimensional table of any database.
When you try to retrieve some data using SELECT statement, then the database will 1st create a CURSOR object and return its reference to you.
The pointer of this returned reference is pointing to the 0th location which is otherwise called as before the first location of the Cursor, so when you want to retrieve data from the cursor, you have to 1st move to the 1st record so we have to use moveToFirst
When you invoke moveToFirst() method on the Cursor, it takes the cursor pointer to the 1st location. Now you can access the data present in the 1st record
The best way to look :
Cursor cursor
for (cursor.moveToFirst();
!cursor.isAfterLast();
cursor.moveToNext()) {
.........
}
if (cursor.getCount() == 0)
return;
cursor.moveToFirst();
while (!cursor.isAfterLast())
{
// do something
cursor.moveToNext();
}
cursor.close();
Initially cursor is not on the first row show using moveToNext() you can iterate the cursor when record is not exist then it return false,unless it return true,
while (cursor.moveToNext()) {
...
}

Pattern for querying the database asynchronously in getChildrenCursor() method of CursorTreeAdapter class

I've implemented a custom Adapter for a ExpandableListView which I extended from the CursorTreeAdapter class. Everything is working as expected.
But I'm wondering if there's pattern or some kind of best practice on how to asynchronously query the database in the getChildrenCursor() method of the adapter class. At the moment I'm passing my SQLiteOpenHelper class to the constructor of my adapter and use it in getChildrenCursor() to query the database synchronously on the UI thread.
You could also use a CursorLoader instead of subclassing AsyncTask to asynchronously query a provider.
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (id != -1) {
// child cursor
return new CursorLoader(getActivity(), childrenUri,
CHILDREN_PROJECTION, selection, selectionArgs, sortOrder);
} else {
// group cursor
return new CursorLoader(getActivity(), groupsUri,
GROUPS_PROJECTION, selection, null, sortOrder);
}
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
int id = loader.getId();
if (id != -1) {
// child cursor
if (!data.isClosed()) {
try {
mAdapter.setChildrenCursor(id, data);
} catch (NullPointerException e) {
Log.w("TAG",
"Adapter expired, try again on the next query: "
+ e.getMessage());
}
}
} else {
// group cursor
mAdapter.setGroupCursor(data);
}
}
public void onLoaderReset(Loader<Cursor> loader) {
int id = loader.getId();
if (id != -1) {
// child cursor
mAdapter.setChildrenCursor(id, null);
} else {
// group cursor
mAdapter.setGroupCursor(null);
}
}
And in your adapter class you can override the getChildrenCursor() method like this:
protected Cursor getChildrenCursor(Cursor groupCursor) {
// Given the group, we return a cursor for all the children within that group
int id = groupCursor.getInt(groupCursor
.getColumnIndex(ContactsContract.Groups._ID));
mActivity.getLoaderManager().initLoader(id, null,mFragment);
return null;
}
getChildrenCursor says:
If you want to asynchronously query a
provider to prevent blocking the UI,
it is possible to return null and at a
later time call setChildrenCursor(int,
Cursor).
So, in getChildrenCursor(), start an AsyncTask and return null. In the onPostExecute() method call setChildrenCursor()

Categories

Resources