Move db queries out of global scope.

Change-Id: Ibcb5ec01a67c41a45c43460a4caa3266244f00c7