0

The beginning of my settings.py has the following.

import os, sys

PROJECT_PATH            = os.path.dirname(os.path.abspath(__file__))
CURRENT_DIR             = os.path.dirname(__file__)
TEMPLATE_DIRS           = (os.path.join(CURRENT_DIR, 'templates'),)
STATICFILES_DIRS        = (os.path.join(CURRENT_DIR, 'static'),)

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
  # ('Your Name', '[email protected]'),
)

MANAGERS = ADMINS

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.sqlite3', 
    'NAME': '/home/ubuntu/myapp/myapp',       
  }
}

I get the following in my error log after a call from views.py to the database.

[time] [error]   File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 372, in count
[time] [error]     return self.query.get_count(using=self.db)
[time] [error]   File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 423, in get_count
[time] [error]     number = obj.get_aggregation(using=using)[None]
[time] [error]   File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 389, in get_aggregation
[time] [error]     result = query.get_compiler(using).execute_sql(SINGLE)
[time] [error]   File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 839, in execute_sql
[time] [error]     cursor = self.connection.cursor()
[time] [error]   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 324, in cursor
[time] [error]     cursor = self.make_debug_cursor(self._cursor())
[time] [error]   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 306, in _cursor
[time] [error]     self._sqlite_create_connection()
[time] [error]   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 296, in _sqlite_create_connection
[time] [error]     self.connection = Database.connect(**kwargs)
[time] [error] OperationalError: unable to open database file

How do I go about correctly referring to my database file in settings.py?

3
  • are you running this with uwsgi/guinicorn? does that process have permission to access the database file?
    – Thomas
    Aug 6, 2013 at 3:33
  • there is a wsgi.py file
    – jeff
    Aug 6, 2013 at 12:51
  • are you using apache/wsgi or a separate wsgi server like uwsgi or gunicorn?
    – Thomas
    Aug 7, 2013 at 0:48

2 Answers 2

0

You need to run python manage.py syncdb, as that process will create the database before you can access it in views.py or anywhere else.

3
  • I ran python manage.py syncdb and I get (use ./manage.py migrate to migrate these)
    – jeff
    Aug 6, 2013 at 12:51
  • Because you are running south. Aug 6, 2013 at 15:39
  • I deleted the database file myapp and ran python manage.py syncdb again and did not get the above error but I still get the OperationalError at runtime.
    – jeff
    Aug 6, 2013 at 17:20
0

'NAME': '/home/ubuntu/myapp/myapp' looks to me like you're referring to a folder instead of a file. For SQLite, the database name must be a full path, including the name of a file, so, as you already got your project's folder programmatically, you could use that to construct the file path:

'NAME': os.path.join(PROJECT_PATH, 'mydb.db')

4
  • In Linux systems, files don't need extensions. Aug 6, 2013 at 6:23
  • true, but it still looks to me as if he specified myapp the name of his virtualenv, and the next myapp is the actual project folder...although im just guessing here, could be wrong yes...
    – andrean
    Aug 6, 2013 at 6:29
  • When I run file myapp in the root of my django repository I get myapp SQLite 3.x database
    – jeff
    Aug 6, 2013 at 12:54
  • I tried 'NAME': os.path.join(PROJECT_PATH, 'mydb.db') and get the same error
    – jeff
    Aug 6, 2013 at 13:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.