0
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "D:\PythonPack\lib\site-packages\django\core\management\__init__.py", lin
e 443, in execute_from_command_line
    utility.execute()
  File "D:\PythonPack\lib\site-packages\django\core\management\__init__.py", lin
e 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\PythonPack\lib\site-packages\django\core\management\base.py", line 19
6, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "D:\PythonPack\lib\site-packages\django\core\management\base.py", line 23
2, in execute
    output = self.handle(*args, **options)
  File "D:\PythonPack\lib\site-packages\django\core\management\base.py", line 30
4, in handle
    app_output = self.handle_app(app, **options)
  File "D:\PythonPack\lib\site-packages\django\core\management\commands\sqlall.p
y", line 19, in handle_app
    return u'\n'.join(sql_all(app, self.style, connections[options.get('database
')])).encode('utf-8')
  File "D:\PythonPack\lib\site-packages\django\core\management\sql.py", line 145
, in sql_all
    return sql_create(app, style, connection) + sql_custom(app, style, connectio
n) + sql_indexes(app, style, connection)
  File "D:\PythonPack\lib\site-packages\django\core\management\sql.py", line 26,
 in sql_create
    tables = connection.introspection.table_names()
  File "D:\PythonPack\lib\site-packages\django\db\backends\__init__.py", line 89
5, in table_names
    cursor = self.connection.cursor()
  File "D:\PythonPack\lib\site-packages\django\db\backends\__init__.py", line 30
6, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "D:\PythonPack\lib\site-packages\django\db\backends\sqlite3\base.py", lin
e 281, in _cursor
    self._sqlite_create_connection()
  File "D:\PythonPack\lib\site-packages\django\db\backends\sqlite3\base.py", lin
e 271, in _sqlite_create_connection
    self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file

我按照本教程https://docs.djangoproject.com/en/dev/intro/tutorial01/尝试执行命令时遇到上述错误

python manage.py sql polls

任何人都知道可能是什么问题?十分感谢。

[更新]*强文本*经理=管理员

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'E:\Python\mysite\sqlitedb', # 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.
    }
}



INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'polls',
)
4

1 回答 1

2

Sqlite3 需要数据库文件的完整路径。确保您的数据库名称不是相对路径。

应该是这样的:

import os
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite',
        'NAME': os.path.join(PROJECT_ROOT, 'dev.db'),
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

还要确保该文件没有太严格的权限。

于 2012-06-24T13:36:45.570 回答