Reputation: 9
I am confused... I have a file "calculation.py" which contains which contains following code:
conn = sqlite3.connect('matchprediction.db')
c = conn.cursor()
c.execute("SELECT * FROM spielplan WHERE table = 8")
In addition I run that file via the local host in django to simulate a webserver.
Whenever I start the server with python manage.py runserver, I get the error message:
sqlite3.OperationalError: no such table: table
The database definitely exists and the table itself, too. Have checked it with a DB Browser. The calculations.py works, because it retrieves data from this table to execute the calculation. The output result in the run-window is correct.
What am I doing wrong and how may I fix this?
Upvotes: 0
Views: 1675
Reputation: 169338
Well, I have two matchprediction.db. One in the project folder (empty) and one in the app folder (contains table and its content).
The empty one in the project folder was created by the Django server being run with the project folder as the working directory and you calling sqlite3.connect()
with a relative path.
If you run (or have run) app/calculation.py
on the command line, the working directory for that script will be app/
.
If you want to make this more bullet-proof, you can form the true path to the database with
import os
database_path = os.path.join(os.path.dirname(__file__), 'matchprediction.db')
conn = sqlite3.connect(database_path)
– i.e. always anchor matchprediction.db
to the module path that has that database_path =
line instead of the working directory.
Upvotes: 0