Fix a number of db query issues in GalleryDatabaseHelper
Don't check query.size() for no good reason.
Prepare the query outside the for loop, this is much more efficient.
Prepare queries outside the while loops. Much more efficient.