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

发布于 2024-12-23 21:04:49 字数 815 浏览 1 评论 0原文

我想将 SimpleCursorAdapter 与 Spinner 一起使用。

我找到了如何返回游标。

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 ?

我对所有解决方案持开放

态度

I'd like to use a SimpleCursorAdapter with a Spinner.

I found how to return a 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;

But the Spinner require a _id field and I'll only have an object with an id field. I prefer to avoid the rename of the field.

How can I resolve that case ? I really need to associate an id to all spinner field.

I imagined that I can maybe issue a cursor from a rawsql but I din't find how with ormlite. It seems to be possible if I can create a PreparedQuery with a raw sql.

I also read that if I have an AndroidDatabase object I can issue a Cursor object but how can we create an AndroidDatabase with ormlite ?

I'm really open with all the solution

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失退 2024-12-30 21:04:49

您可以使用 QueryBuilderORMLite 获取底层 Cursor 对象> 无需诉诸原始查询。看看这个答案:

Android 光标与 ORMLite 在 CursorAdapter 中使用< /a>


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

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

You can get the underlying Cursor object from ORMLite by using QueryBuilder without having to resort to a raw query. Take a look at this answer:

Android Cursor with ORMLite to use in CursorAdapter

You can do 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();
}
转角预定愛 2024-12-30 21:04:49

嗯,我刚刚找到了一个似乎高效、简单且符合 Ormlite 的解决方案。

我只需使用 getHelper().getReadableDatabase() 获取 AndroidDatabase 即可。

然后使用

Cursor cursor = db.query("choixpointverification",
    new String[] { "id", "id as _id", "nom" },
    "masque = 0 and idPointVerification = " + idPointVerification.toString(),
    null, null, null, "tri");

Well I just found a solution which seems to be efficient, simple, and compliant with ormlite.

I just have to get an AndroidDatabase with getHelper().getReadableDatabase().

and then use

Cursor cursor = db.query("choixpointverification",
    new String[] { "id", "id as _id", "nom" },
    "masque = 0 and idPointVerification = " + idPointVerification.toString(),
    null, null, null, "tri");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文