SQLiteQueryBuilder.query() vs SQLiteDatabase.query()

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 12:12:16

问题


What's the difference between these two approaches? For me it seems that the only benefit of SQLiteDatabase is its ability to work with different databases. Am I right?


回答1:


The primary method is SQLiteDatabase#rawQuery(). Both SQLiteDatabase#query() and SQLiteQueryBuilder are just helpers to compose the SQL.

The SQLiteDatabase#query() can only compose simple queries from one table. The SQLiteQueryBuilder can create joins, unions and such. Since SQLiteQueryBuilder is an extra object, you'd only construct it if you need it's power.

Personally I think that any non-trivial SQL is easier to read as SQL than as pieces composed with helper like this, so I'd use rawQuery over SQLiteQueryBuilder, but that's a matter of taste and how well you know SQL. The SQLiteQueryBuilder might also be useful if you have some common subqueries that you want to compose together in different ways.

In fact I would prefer to use prepared statements, because compilation of SQL is slow compared to it's execution, and because it avoids doing string operations on potentially untrusted values. I couldn't find the API at first, because for some strange reason it is called SQLiteDatabase#compileStatement rather than the usual prepare used in the underlying C API.




回答2:


SQLiteQueryBuilder is useful if you want to do joins on multiple tables. It has several convenience methods for that, if you view the source-code on GrepCode: SQLiteQueryBuilder

Otherwise, I cannot think of a solid reason to use SQLiteQueryBuilder over other approaches for querying the database.



来源:https://stackoverflow.com/questions/17230049/sqlitequerybuilder-query-vs-sqlitedatabase-query

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