java 在android中使用sqlite连接表的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3277908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:07:06  来源:igfitidea点击:

Best way to join tables using sqlite in android

javaandroidsqlitejoincursor

提问by Dale Marshall

I am trying to find out the best way to do a simple table join on my two tables using a sqlite database in an android application. Is the simplest way to use CursorJoiner or is there any easier way?

我正在尝试找出在 android 应用程序中使用 sqlite 数据库对我的两个表进行简单表连接的最佳方法。是使用 CursorJoiner 的最简单方法还是有更简单的方法?

回答by Tim Kryger

In the implementation of SQLiteDatabaseand SQLiteQueryBuilderyou will see that it is possible to pass the tables you want to join to the tableargument of queryeven though the documentation implies it will only take a single name of a table. The documentation for SQLiteQueryBuilderis more clear and even suggests things like foo, baror foo LEFT OUTER JOIN bar ON (foo.id = bar.foo_id).

SQLiteDatabaseSQLiteQueryBuilder的实现中,您将看到可以将要加入的表传递给table参数,query即使文档暗示它只会采用表的单个名称。SQLiteQueryBuilder的文档更加清晰,甚至建议了诸如foo, bar或 之类的内容foo LEFT OUTER JOIN bar ON (foo.id = bar.foo_id)

回答by bigsolom

If you're willing to use a third-party library, I recommend using Active Android

如果您愿意使用第三方库,我建议您使用 Active Android

https://github.com/pardom/ActiveAndroid

https://github.com/pardom/ActiveAndroid

using it you can merge tables like this

使用它你可以像这样合并表

From query = new Select()
    .from(Foo.class)
    .innerJoin(Bar.class)
    .on("Foo.Id=Bar.Id");

you can even get a cursor back to use in loaders

你甚至可以让一个游标回到加载器中使用

Cursor cursor = Cache.openDatabase().rawQuery(query.toSql(), query.getArguments());

more details here

更多细节在这里