Simplify sql slightly

Move (re-)setting of $db to conditional, don't duplicate command line

Change-Id: I8796816b5ff279bf6687175f759b250838ef8959
Merge "Simplify sql slightly" into production