writing DB routine