tweaked code to handle dict and sqlite.