Best way to join tables using sqlite in android

你离开我真会死。 提交于 2019-12-20 10:25:00

问题


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?


回答1:


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




回答2:


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

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



来源:https://stackoverflow.com/questions/3277908/best-way-to-join-tables-using-sqlite-in-android

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