0

Is there a way to run a python script that will add data to this SQLite DB then? I mean, a completely separate .py file that I will run in the shell, doing python thefile.py in an SSH connection?

Of course, this file will be on the same server as the whole Django project.

This way, I can easily access the data from the DB from my Django project, and display it the way I want in a pretty HTML/CSS web page.

Thanks

Michael
  • 43
  • 2
  • 9
  • You can, but you shouldn't. You should also access to Django's database through Django. – janos Jun 04 '17 at 20:52
  • I'm already accessing Django's DB from the Django project. Can you answer my question, or just link me to some docs explaining on how to do what I want ? Thanks – Michael Jun 04 '17 at 20:57
  • You could use the sqlite3 Python library or something like SQLAlchemy. The tables generated by Django are fairly straightforward, so reading them should be OK. But I agree with @janos that writing to them might not be a good idea. – TimGJ Jun 04 '17 at 20:57
  • Thanks @TimGJ for your answer. In the SQLite3 docs, it's written that, in order to establish the connection, I have to do `sqlite3.connect('example.db')` first, but how can I know what to put instead of 'example.db' ? – Michael Jun 04 '17 at 21:00
  • IIRC, look in the root directory of your Django project. It will have generated a .db file there. That's the one you want. – TimGJ Jun 04 '17 at 21:04
  • Thanks. Why did you guys tell me not to do it this way? – Michael Jun 04 '17 at 21:17
  • Possible duplicate of [Using django for CLI tool](https://stackoverflow.com/questions/32088702/using-django-for-cli-tool) – e4c5 Jun 06 '17 at 14:31

1 Answers1

0

Is there a reason for not using django? you can add a command to the manage.py the way it's described here https://docs.djangoproject.com/en/1.11/howto/custom-management-commands/

moshe742
  • 1
  • 3
  • Because I need to run the python script once every hour. I use Cron to do that. Can I do it with Django? – Michael Jun 04 '17 at 21:55
  • You can run the command you built like the link in the documentation and run it from cron. You just need to go to the directory your Django is in and from there do the python manage.py your_command – moshe742 Jun 05 '17 at 22:21