Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLiteQueryBuilder.query() vs SQLiteDatabase.query()

Tags:

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?

like image 426
user1049280 Avatar asked Jun 21 '13 07:06

user1049280


2 Answers

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.

like image 66
Jan Hudec Avatar answered Sep 23 '22 08:09

Jan Hudec


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.

like image 42
IgorGanapolsky Avatar answered Sep 25 '22 08:09

IgorGanapolsky