move db execution code into single function