django.db.utils.OperationalError:无法打开数据库文件

时间:2016-12-04 13:54:41

标签: django

当我跑步时

python manage.py runserver

我收到此错误

文件“/usr/local/lib/python2.7/dist-packages/Django-1.10.1-py2.7.egg/django/db/backends/sqlite3/base.py”,第209行,在get_new_connection中     conn = Database.connect(** conn_params) django.db.utils.OperationalError:无法打开数据库文件

我的settings.py:

DATABASES = {
    'default': dj_database_url.config(
        default="sqlite:///{}".format(
            os.path.join(BASE_DIR, 'db/db.sqlite3')
        )
    )
}

9 个答案:

答案 0 :(得分:5)

从同一个问题中苦苦挣扎了一段时间,我相信我终于找到了解决方案!

str_regex = '(https?:\/\/)?([a-z]+\d\.)?([a-z]+\.)?activeingredients\.[a-z]+(/?(work|about|contact)?/?([a-zA-z-]+)*)?/?'

import urllib.request
from Stacks import Stack
import re
import functools
import operator as op
from nary_tree import *
url = 'http://www.activeingredients.com/'
s = set()
List = []
url_list = []
def f_go(List, s, url):
    try:
        if url in s:
            return
        s.add(url)
        with urllib.request.urlopen(url) as response:
            html = response.read()
            #print(url)
        h = html.decode("utf-8")
        lst0 = prepare_expression(list(h))
        ntr = buildNaryParseTree(lst0)
        lst2 = nary_tree_tolist(ntr)
        lst3= functools.reduce(op.add, lst2, [])
        str2 = ''.join(lst3)
        List.append(str2)
        f1 = re.finditer(str_regex, h)

        l1 = []
        for tok in f1:
            ind1 = tok.span()
            l1.append(h[ind1[0]:ind1[1]])
    for exp in l1:
        length = len(l1)
        if (exp[-1] == 'g' and exp[length - 2] == 'p' and exp[length - 3] == 'j')  or \
            (exp[-1] == 'p' and exp[length - 2] == 'n' and exp[length - 3] == 'g'):
                pass
        else:
            f_go(List, s, exp, iter_cnt + 1, url_list)
except:
    return

这对我有用。

我的权限告诉我一切都应该如何。无论如何,我仍然必须在终端的命令开头输入 sudo

答案 1 :(得分:2)

基本上有两个答案,即运行服务器的用户无权打开数据库文件。 您可以尝试通过以下方式解决此问题:

sudo chown $(whoami):$(whoami) /path/to/dir/db/db.sqlite3

或者您没有此文件,您可以通过应用migrate命令创建它:

./manage.py migrate

答案 2 :(得分:1)

我有同样的问题,刚解决了。确保www-data(或运行Web服务器的任何守护程序)都可以访问db.sqlite3文件以及它的路径。所以:

sudo chown :www-data <project_folder>/
sudo chown :www-data <project_folder>/db.sqlite3
sudo chmod 664 <project_folder>/db.sqlite3

答案 3 :(得分:0)

只需删除settings.py文件中的第一个数据库。

如果您删除了第一个os.path.join(BASE_DIR, 'db/db.sqlite3'),则您有db,您将拥有os.path.join(BASE_DIR, 'db.sqlite3')

您的数据库设置将是

DATABASES = {
    'default': dj_database_url.config(
        default="sqlite:///{}".format(
            os.path.join(BASE_DIR, 'db.sqlite3')
        )
    )
}

答案 4 :(得分:0)

对我来说,问题是我有两个设置文件;一种用于生产,一种用于开发。在我的manage.py中,我指定了部署设置文件,却忘记了将manage.py添加到我的.gitignore中,因此,当我在本地运行该项目时,它在尝试查找生产数据库时失败。

答案 5 :(得分:0)

对于任何Windows用户:

在创建第一个django项目时遇到了相同的错误。我无法在Windows上使用<div class="cell text_cell unrendered unselected" tabindex="2"> <div class="prompt input_prompt"></div> <div class="inner_cell"> <div class="ctb_hideshow"> <div class="celltoolbar"></div> </div> <div class="input_area" aria-label="Edit Markup Text here"> <div class="CodeMirror cm-s-default CodeMirror-wrap"> <div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59999px; left: 5.6px;"> <textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea> </div> <div class="CodeMirror-vscrollbar" cm-not-content="true"> <div style="min-width: 1px; height: 0px;"></div> </div> <div class="CodeMirror-hscrollbar" cm-not-content="true"> <div style="height: 100%; min-height: 1px; width: 0px;"></div> </div> <div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div> <div class="CodeMirror-gutter-filler" cm-not-content="true"></div> <div class="CodeMirror-scroll" tabindex="-1"> <div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: -12px; border-right-width: 18px; min-height: 28px; padding-right: 0px; padding-bottom: 0px;"> <div style="position: relative; top: 0px;"> <div class="CodeMirror-lines" role="presentation"> <div role="presentation" style="position: relative; outline: none;"> <div class="CodeMirror-measure"></div> <div class="CodeMirror-measure"></div> <div style="position: relative; z-index: 1;"></div> <div class="CodeMirror-cursors"> <div class="CodeMirror-cursor" style="left: 5.6px; top: 0px; height: 16.8px;"> &nbsp;</div> </div> <div class="CodeMirror-code" role="presentation"> <pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre> </div> </div> </div> </div> </div> <div style="position: absolute; height: 18px; width: 1px; border-bottom: 0px solid transparent; top: 28px;"> </div> <div class="CodeMirror-gutters" style="display: none; height: 46px;"></div> </div> </div> </div> <div class="text_cell_render rendered_html" tabindex="-1"> <p>Type <em>Markdown</em> and LaTeX: <span class="MathJax_Preview" style="color: inherit;"><span class="MJXp-math" id="MJXp-Span-1"><span class="MJXp-msubsup" id="MJXp-Span-2"><span class="MJXp-mi MJXp-italic" id="MJXp-Span-3" style="margin-right: 0.05em;">α</span><span class="MJXp-mn MJXp-script" id="MJXp-Span-4" style="vertical-align: 0.5em;">2</span></span></span></span> <script type="math/tex" id="MathJax-Element-1">\alpha^2</script> </p> </div> </div> </div> ,因此命令sudo仅在以管理员身份运行终端时才有效。

答案 6 :(得分:0)

如果您在AWS EC2节点上遇到此问题并使用apache,则必须解决以下问题:

chown -R apache:apache project_folder

答案 7 :(得分:0)

我有类似的问题。

您只需使用 sudo 运行命令

sudo python3 manage.py runserver

享受吧!

答案 8 :(得分:-1)

@ whinytween96:我已经看到当sudo用于运行某些命令而不是其他命令时,会出现此问题。您需要与sudo的使用保持一致才能解决此问题。我有数据库问题,我通过再次运行sudo前缀命令来修复它。

即。

  1. sudo python manage.py runserver
  2. sudo python manage.py makemigrations
  3. sudo python manage.py migrate
  4. 为我解决了这个问题。

相关问题