Django新手: python manage. pysyncdb错误?

标签 django sqlite

目前正在研究 Django 教程,当我尝试时遇到此错误:

python manage.py syncdb

使用sqlite3

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 371, in handle
    return self.handle_noargs(**options)
  File "/Library/Python/2.6/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
    cursor = connection.cursor()
  File "/Library/Python/2.6/site-packages/django/db/backends/__init__.py", line 306, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/Library/Python/2.6/site-packages/django/db/backends/sqlite3/base.py", line 281, in _cursor
    self._sqlite_create_connection()
  File "/Library/Python/2.6/site-packages/django/db/backends/sqlite3/base.py", line 271, in _sqlite_create_connection
    self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file

我的settings.py 文件内容如下:

        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '/Users/username/django_tutorial/mysite', 

有人知道这里的问题吗?好像我放入了完整的文件目录,但数据库仍然无法创建? ):

最佳答案

您应该使用文件的路径,包括文件名:

    'NAME': '/Users/username/django_tutorial/mysite/data.sqlite', 

您也可以只给它文件名:

    'NAME': 'data.sqlite',

在这种情况下,它将在与运行 manage.py 的目录相同的目录中创建。

最佳实践是传递绝对路径,但要确保该绝对路径是相对于设置文件创建的。例如:

import os

SETTINGS_DIR = os.path.dirname(os.path.abspath(__file__))

然后是后者:

    'NAME': os.path.join(SETTINGS_DIR, 'data.sqlite'),

请记住,设置文件是一个 Python 文件,您可以在其中使用 Python 代码。

关于Django新手: python manage. pysyncdb错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10807174/

相关文章:

python - 如何获取模板中的 "price_retail"字段值?

sqlite - 为什么对 Java 应用程序使用 SQLite 而不是 H2

mysql - SQLite - 从表中检索名称集

python - 使用 python 在 SQLite 数据库中重复写入数据

python - 如何通过 django-microsoft-auth 使用 Azure AD 在 Django 中进行身份验证

python - 如何禁用 Nose 测试的覆盖率报告

python - Django 值从 DateTimeField 获取年份

python - GeoDjango--Python :Can't Display Map

Android App : can I use iOS sqlite? 如果没有,我如何从云 (Parse.com) 中的数据库创建本地数据库?

java - Eclipse 中的 Android 'create table if not exist' 错误