34

Is there any way to get Cursor for a query, which I am processing with ORMLite Dao object?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
sealskej
  • 7,281
  • 12
  • 53
  • 64

4 Answers4

65

ORMLite now supports next(), previous(), moveRelative(offset), ... methods on the CloseableIterator class. This should allow you to move the underlying Cursor object around at will.

It also supports the following DAO Cursor methods:


When you are building your own query with ORMLite, you use the QueryBuilder object. queryBuilder.prepare() returns a PreparedQuery which is used by various methods in the DAO. You can call dao.iterator(preparedQuery) which will return a CloseableIterator which is used to iterate through the results. There is a iterator.getRawResults() to get access to the DatabaseResults class. Under Android, this can be cast to an AndroidDatabaseResults which has a getCursor() method on it to return the Android Cursor.

Something like the following code:

// build your query
QueryBuilder<Foo, String> qb = fooDao.queryBuilder();
qb.where()...;
// when you are done, prepare your query and build an iterator
CloseableIterator<Foo> iterator = dao.iterator(qb.prepare());
try {
   // get the raw results which can be cast under Android
   AndroidDatabaseResults results =
       (AndroidDatabaseResults)iterator.getRawResults();
   Cursor cursor = results.getRawCursor();
   ...
} finally {
   iterator.closeQuietly();
}

This is a bit complicated but you are definitely having to peer behind the vale to get to this object which is hidden by the database abstraction classes.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • 1
    how can i obtain the databaseConnection? – max4ever Jan 19 '12 at 12:10
  • I've changed this answer to show how it can be done without needed the database connection. – Gray Sep 14 '12 at 20:05
  • @Gray What is the best way to inflate the list_item views using the cursor? Also, is a `CursorAdapter` the best thing to use with `OrmLiteBaseListActivity`? – theblang Oct 30 '13 at 16:00
  • Sorry @mattblang. I'm not an Android developer. I'd ask that question on the mailing list: https://groups.google.com/forum/#!forum/ormlite-android – Gray Oct 30 '13 at 20:09
  • 3
    @Gray your contribution on SO specifically regarding ORMLite is much appreciated, tnx – Dakait Nov 18 '13 at 22:41
  • 5
    @Gray when using this strategy in and handing the cursor off to a `SimpleCursorAdapter` or `CursorAdapter` subclass of my own used with an Android Grid/ListView, I get a `StaleDataException` -- I noticed commenting out the `iterator.closeQuietly()` prevents this from happening. What would you suggest to work-around this -- apart from keeping the iterator around until I'm done with the cursor? – Mark Jan 30 '14 at 12:31
  • 1
    @Mark Yes, same problem here. What did you end up doing? – theblang Apr 13 '14 at 05:36
0

Did you try some of Gray's advice from this post? He explains how you can select a column as another name, such as, select id as _id.

Community
  • 1
  • 1
Simon.Ponder
  • 492
  • 6
  • 14
  • Since you can name your columns in ormlite I guess why not just name them _id, rather than doing the select as :) ? – AgentKnopf Sep 09 '14 at 19:07
  • Sure, you can, but in an instance where renaming them is not an option, you simply use the sql syntax of "as";) – Simon.Ponder Sep 16 '14 at 12:24
0

If you're in an Activity and don't want to mess around with the QueryBuilder give the following a go, which is just as effective.

Cursor cursor = getHelper().getReadableDatabase().query(tableName, projection, selection, selectionArgs, groupBy, having, sortOrder)
jklp
  • 2,091
  • 2
  • 23
  • 37
0

If you mean the getHelper() method to reach the dao methods create etc. you only have to inherit from the OrmLiteBaseActivity<YourDBHelper> and you can call it. It will look sth like this:

public class YourClass extends OrmLiteBaseActivity<YourDBHelper> {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
     ...
     getHelper().getDao().queryForAll();
     ...
  }
}

If you mean the cursor to handle database operation: I don't think that you can reach it! But I don't understand why you should need it. ORMLite has nearly all functions of the cursor. So what do you need it for?

Taryn
  • 242,637
  • 56
  • 362
  • 405
Vince
  • 11
  • 2