[Django]-Where is the sqlite database file created by Django?

43πŸ‘

βœ…

In the settings.py file, there is a variable called DATABASES. It is a dict, and one of its keys is default, which maps to another dict. This sub-dict has a key, NAME, which has the path of the SQLite database.

This is an example of a project of mine:

CURRENT_DIR= '/Users/brandizzi/Documents/software/netunong'
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': CURRENT_DIR+ '/database.db', # <- The path
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

You can easily retrieve this value using the Django shell that is accessible running the command python manage.py shell. Just follow the steps below:

>>> import settings
>>> settings.DATABASES['default']['NAME']
'/Users/brandizzi/Documents/software/netunong/database.db'

If the returned value is some relative path, just use os.path.abspath to find the absolute one:

>>> import os.path
>>> os.path.abspath(settings.DATABASES['default']['NAME'])
'/Users/brandizzi/Documents/software/netunong/database.db'
πŸ‘€brandizzi

0πŸ‘

if settings not available then this could embedded in your packageName/base Directory:

try:

import packageName
import os.path.abspath
 In [3]: os.path.abspath(packageName.DATABASES['default']['NAME'])`
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-ca6dcbd75c6d> in <module>()
----> 1 os.path.abspath(settings.DATABASES['default']['NAME'])
>>> NameError: name 'settings' is not defined
>>> os.path.abspath(packageName.settings.DATABASES['default']['NAME'])`
>>> '/Users/brandizzi/Documents/software/netunong/database.db'
πŸ‘€shashankS

Leave a comment