Question: Why can\'t I open the database?
Info: I\'m working on a project using sqlite3
database. I wrote a test program that runs and passes it t
The only thing you need to do is create the folder (as it doesn't exist already), only the database file will be created by the program. This really worked for me!
Use the fully classified name of database file
Use- /home/ankit/Desktop/DS/Week-7-MachineLearning/Week-7-MachineLearning/soccer/database.sqlite
instead-
One reason might be running the code in a path that doesn't match with your specified path for the database. For example if in your code you have:
conn = lite.connect('folder_A/my_database.db')
And you run the code inside the folder_A
or other places that doesn't have a folder_A
it will raise such error. The reason is that SQLite will create the database file if it doesn't exist not the folder.
One other way for getting around this problem might be wrapping your connecting command in a try-except
expression and creating the directory if it raises sqlite3.OperationalError
.
from os import mkdir import sqlite3 as lite
try:
conn = lite.connect('folder_A/my_database.db')
except lite.OperationalError:
mkdir('folder_A')
finally:
conn = lite.connect('folder_A/my_database.db')
had the same problem but top answer is too long for me so I suggest to open another shell window type
cd #enter
and try again
Make sure you are not editing the settings.py file while trying to run syncdb, you will get the same error!!!
self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file
in my case, i tried creating the sqlite db in /tmp
folder and from all the slashes i missed a single slash
Instead of sqlite:///tmp/mydb.sqlite
-> sqlite:////tmp/mydb.sqlite
...