database only after connecting

How can I store data in a file inside the phone to send it to a database only after connecting the internet? 

Can anyone help me set this part of the program? 

I’m still learning and I’m very complicated … 

create file 

save, save, save (N times) 

If connected then send

…If sent correctly then delete file 

…If not submitted properly then keep file.