Reading data from database and saving in a csv file