ORMLite with CursorAdapter in Android

醉酒当歌 提交于 2019-11-28 09:13:03
Gray

Your comments indicate that you've already answered you problem. You can certainly create a column named "_id" using ORMLite:

@DatabaseField(generatedId = true)
private int _id;

or

@DatabaseField(generatedId = true, columnName = "_id")
private int id;

If you are working with Cursors then you may want to take a look at the last() and moveAbsolute(...) methods on the DatabaseResults class. Also, the AndroidDatabaseResults (which you can cast to) also has a getRawCursor() method which returns the underlying Cursor object and has additional getCount() and getPosition() methods.

Here are some more information about ORMLite and Cursors:

Android Cursor with ORMLite to use in CursorAdapter

You can get access to the Cursor using something like the following:

// 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();
}
Stev_k

It turns out I did need a raw SQL query with ORMLite after all as I needed to do a Left Join, (i.e. not as a result of having to rename the id column in a query, that is not necessary as per Gray's answer above)

The way I did it is below:

public class DatabaseHelper extends OrmLiteSqliteOpenHelper{
   public void myRawQueryMethod() {
        SQLiteDatabase database = this.getReadableDatabase();
        String SqlQuery = "Write raw SQL query here"; 
        Cursor cursor = database.rawQuery(SqlQuery, null);
    }
}

Thanks for advice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!