in reply to Reading from databse and writing to file.
Perhaps if you stated what was going wrong or not behaving as you expected someone might have a clue where to start offering advice . . .