Improve runtime database query functions

No longer susceptible to the `.next()[0]` problem when `.next()` is returning `None`.

(This pull request contains more changes that I have done as part of the log and work restructure change, but should really be separate.)