Move db queries out of global scope.

Change-Id: I8f0ee4c870c2742653df2d668a31b110cf33fd73