0

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?

3
  • Are you sure it's opening the right matchprediction.db? Since you're not using an absolute path, it's relative to the working directory of the server, which could be anything. Commented Oct 18, 2021 at 7:27
  • Well, I have two matchprediction.db. One in the project folder (empty) and one in the app folder (contains table and its content). I guess it's the right matchprediction.db, cuz otherwise the calculation in the script would not work. If I hover over the matchprediction.py, copy the path, browse to that path and open that specific matchprediction.py it contains the table 'spielplan' and all data I need. Commented Oct 18, 2021 at 7:37
  • It appears that your table spielplan has a column named table. But in most dialects of SQL, table is a reserved word (as in create table). Commented Oct 18, 2021 at 7:50

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That works for me. Thank you soooo much! unfortunately can't upvote you yet. But will consider you in my bedtime prays :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.