【问题标题】:Python and Django fails to run [python manage.py sql application]Python 和 Django 运行失败【python manage.py sql application】
【发布时间】:2012-06-26 00:49:34
【问题描述】:
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

有人知道可能是什么问题吗?非常感谢。

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

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',
)

【问题讨论】:

  • 用您的settings.py 文件更新问题。

标签: python django django-models python-3.x django-admin


【解决方案1】:

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

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

【讨论】:

  • 比我要写的要好。编辑添加导入行
  • 谢谢 Yuval 我按照教程创建模型文件并运行python manage.py sql polls,这将为我创建 db 文件,我想。所以我不需要指定db文件的路径
猜你喜欢
  • 1970-01-01
  • 2012-05-20
  • 2016-10-10
  • 1970-01-01
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 2016-02-01
  • 1970-01-01
相关资源
最近更新 更多