moving the write to database

to ensure it is already opened