Improve runtime database query functions

No longer susceptible to `.next()[0]` problem.
Merge pull request #1067 from matthewrmshin/improve-db-query-funcs

Improve runtime database query functions