Django创建的sqlite数据库文件在哪里? [英] Where is the sqlite database file created by Django?

查看:120
本文介绍了Django创建的sqlite数据库文件在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了python并且包含了sqlite ...但是用 manage.py syncdb 创建的sqlite db文件路径在哪里?我在Mac电脑上.

I've got python installed and sqlite is included with it... but where is the sqlite db file path that was created with manage.py syncdb? I'm on a mac.

推荐答案

settings.py 文件中,有一个名为 DATABASES 的变量.这是一个字典,其键之一是 default ,它映射到另一个字典.该下标具有一个键,即 NAME ,该键具有SQLite数据库的路径.

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 subdict 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': '',
    }
}

您可以使用运行命令 python manage.py shell 的Django shell轻松检索此值.只需按照以下步骤操作即可:

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'

如果返回的值是某个相对路径,则只需使用 os.path.abspath 来找到绝对路径:

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'

这篇关于Django创建的sqlite数据库文件在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆