Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite3.OperationalError: unable to open database file

I get this error when setting up a server in Django. It is sqlite3 which means it should create the .db file but it doesn't seem to be doing so. I've stipulated SQLite as the backend and an absolute file path for where to put it, but no luck.

Is this a bug or am I doing something incorrect? (Was just thinking, is the absolute file path specified differently in Ubuntu?)

Here is the beginning of my settings.py file:

# Django settings for OmniCloud project.  DEBUG = True TEMPLATE_DEBUG = DEBUG  ADMINS = ( # ('Your Name', '[email protected]'), )  MANAGERS = ADMINS  DATABASES = { 'default': {     'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.     'NAME': '~/Harold-Server/OmniCloud.db',                      # Or path to database file if using sqlite3.     'USER': '',                      # Not used with sqlite3.     'PASSWORD': '',                  # Not used with sqlite3.     'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.     'PORT': '',                      # Set to empty string for default. Not used with sqlite3. } } 
like image 457
Chris Avatar asked Oct 06 '11 04:10

Chris


People also ask

Why SQLite Cannot open database file?

If SQLite is unable to open the database file, this means that the SQLite database you are trying to open is corrupted. There are various causes of corruption, such as file overwrite issues, file locking issues, database synchronization failures, storage media failures, and many more.

How do I read a .SQLite file in Python?

SQLite Python: Querying Data First, establish a connection to the SQLite database by creating a Connection object. Next, create a Cursor object using the cursor method of the Connection object. Then, execute a SELECT statement. After that, call the fetchall() method of the cursor object to fetch the data.

How do I view tables in SQLite?

If you are running the sqlite3 command-line access program you can type ". tables" to get a list of all tables. Or you can type ". schema" to see the complete database schema including all tables and indices.


2 Answers

Django NewbieMistakes

PROBLEM You're using SQLite3, your DATABASE_NAME is set to the database file's full path, the database file is writeable by Apache, but you still get the above error.

SOLUTION Make sure Apache can also write to the parent directory of the database. SQLite needs to be able to write to this directory.

Make sure each folder of your database file's full path does not start with number, eg. /www/4myweb/db (observed on Windows 2000).

If DATABASE_NAME is set to something like '/Users/yourname/Sites/mydjangoproject/db/db', make sure you've created the 'db' directory first.

Make sure your /tmp directory is world-writable (an unlikely cause as other thing on your system will also not work). ls /tmp -ald should produce drwxrwxrwt ....

Make sure the path to the database specified in settings.py is a full path.

Also make sure the file is present where you expect it to be.

like image 134
John Avatar answered Oct 05 '22 09:10

John


I faced exactly same issue. Here is my setting which worked.

'ENGINE': 'django.db.backends.sqlite3',  'NAME': '/home/path/to/your/db/data.sqlite3' 

Other setting in case of sqlite3 will be same/default.
And you need to create data.sqlite3.

like image 33
chhantyal Avatar answered Oct 05 '22 09:10

chhantyal