Python - when saving data to an sqlite db, then restarting the machine, db is filled with zeros

I have a python script that creates a sqlite db, saves it and closing the connection with close(). Afterwards, I reboot my system (osx) with: mypassword| sudo -S shutdown -r now, shell=True)

What happens , is after the system is rebooted , some of the time (it's not consistent) the db is filled with one or two zeros fields instead of the fields I saved. I tried things like , creating a bash script that will perform the shutdown (and calling it from python-instead of executing shutdown directly), and even call killall python before sending the shutdown command in the bash script, but it still didn't help. For me , it seems like sometimes python keeps some open handles maybe to the db , although I close it correctly and although I can't see the db instance in the memory while using pycharm debugger (I know it doesn't mean the handle is really closed)

I don't know how to continue in solving this from here