Home>
sqlite3.OperationalError: unable to open database file
Corresponding source code

There are utils directory and model directory directly under the A directory, there is a file (test.py) containing the following code in the utils directory, and test.db is directly under the model directory.

# test.py
import sqlite3
import os.path
filepath = os.path.abspath ("../ model/test.db")
conn = sqlite3.connect (filepath)
What I tried
  • I saw an article saying that it is better to use an absolute path instead of a relative path, so I passed the absolute path as above, but it does not change
  • I saw an article that there are not enough permissions, so write permissiontest.dbFile,modelI changed both directories, but they didn't change
drwxrwxrwx @ 5 hogehoge staff 160 8 23 19:05 model
-rwxrwxrwx @ 1 hogehoge staff 21733376 3 27 2020 model/test.db
Supplementary information (FW/tool version, etc.)
  • macOS 10.14.5
  • Python 3.7.4

I get an error when I run test.py, but if anyone knows a solution, please let me know.

  • Answer # 1

    Since I didn't have permission from Python, I set test.db instead of model/test.db and it ran without error.

    (For details, please see the column of "Addition/correction to questions, request for selection of best answer" commented by toast-uz)

  • Answer # 2

    drwxrwxrwx @ 5 hogehoge staff 160 8 23 19:05 model
    -rwxrwxrwx @ 1 hogehoge staff 21733376 3 27 2020 test.db

    It looks like test.db is outside the model directory, is that okay?