android - 使用 ormlite 获取带有原始 sql 的游标

标签 android ormlite

我想将 SimpleCursorAdapter 与微调器一起使用。

我找到了返回 Cursor 的方法。

QueryBuilder<ChoixPointVerification, Integer> qb = choixPointVerificationDao.queryBuilder();
qb.where().eq(FIELD, id);
PreparedQuery<ChoixPointVerification> preparedQuery = qb.prepare();
AndroidCompiledStatement compiledStatement =
                (AndroidCompiledStatement)preparedQuery.compile(db, StatementType.SELECT);

Cursor cursor = compiledStatement.getCursor();
return cursor;

但是 Spinner 需要一个 _id 字段,而我只会有一个带有 id 字段的对象。我更愿意避免重命名该字段。

我该如何解决这个案子?我真的需要将一个 ID 关联到所有微调器字段。

我想我也许可以从 rawsql 发出游标,但我没有找到如何使用 ormlite。如果我可以使用原始 sql 创建一个 PreparedQuery,这似乎是可能的。

我还读到,如果我有一个 AndroidDatabase 对象,我可以发出一个 Cursor 对象,但是我们如何使用 ormlite 创建一个 AndroidDatabase?

我对所有的解决方案都很开放

问候

最佳答案

您可以从 ORMLite 获取底层的 Cursor 对象通过使用 QueryBuilder 而不必求助于原始查询。看看这个答案:

Android Cursor with ORMLite to use in CursorAdapter

您可以执行类似以下代码的操作:

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

关于android - 使用 ormlite 获取带有原始 sql 的游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8694761/

相关文章:

android - IntentService 和 AlarmManager

java - 如何解决 OrmLite 中巨大的 byte[] 问题

android - OrmLite Android 中 int 的默认值

android - Google Fit Error 19 可能的原因及解决方法

android - 通知不会出现在前台服务中

android - 如何通过android中的特定应用程序分别查找wifi和网络数据使用情况?

android - Sqlite 事务阻止 Android ui

java - 使用外国集合字段创建表

mysql - SQLException : Access denied when connecting to mysql in docker

android - Google Play发布失败,出现 “Track names in request path and request body must match”