问题描述
python manage.py syncdb
使用sqlite3
追溯(最近的最后一次调用):
文件manage.py,第10行在< module>
execute_from_command_line(sys.argv)
文件/Library/Python/2.6/site-packages/django/core/management/__init__.py,第443行,在execute_from_command_line
utility.execute ()
文件/Library/Python/2.6/site-packages/django/core/management/__init__.py,第382行,执行
self.fetch_command(子命令).run_from_argv(self。 argv)
文件/Library/Python/2.6/site-packages/django/core/management/base.py,第196行,在run_from_argv
self.execute(* args,** options。 __dict__)
文件/Library/Python/2.6/site-packages/django/core/management/base.py,第232行,执行
output = self.handle(* args,**选项)
文件/Library/Python/2.6/site-packages/django/core/management/base.py,第371行,处理
return self.handle_noargs(** options)
文件/Library/Python/2.6/site-packages/django/core/management/commands/syncdb.py,第57行,handle_noargs
cursor = connection.cursor()
文件 /图书馆/ Python的/ 2.6 / site-packages / django / db / backends / __ init__.py,第306行,光标
cursor = self.make_debug_cursor(self._cursor())
文件/Library/Python/2.6 /site-packages/django/db/backends/sqlite3/base.py,第281行,_cursor
self._sqlite_create_connection()
文件/Library/Python/2.6/site-packages/django /db/backends/sqlite3/base.py,第271行,_sqlite_create_connection
self.connection = Database.connect(** kwargs)
sqlite3.OperationalError:无法打开数据库文件
我的settings.py文件如下:
'ENGINE':'django.db.backends.sqlite3',#添加'postgresql_psycopg2','mysql','sqlite3'或'oracle'。
'NAME':'/ Users / username / django_tutorial / mysite',
这里的问题?看来我正在放入完整的文件目录,但仍然无法创建数据库? ):
您应该使用该文件的路径,包括文件名:
'NAME':'/Users/username/django_tutorial/mysite/data.sqlite',
你也可以给它文件名:
'NAME' :'data.sqlite',
在这种情况下,它将被创建在与一个目录相同的目录中您从中运行 manage.py
。
最佳做法是传递绝对路径,但要确保此绝对路径是相对于设置文件创建的。 EG:
import os
SETTINGS_DIR = os.path.dirname(os.path.abspath __file__))
然后后:
'NAME':os.path.join(SETTINGS_DIR,'data.sqlite'),
请记住,设置文件是一个Python文件,您可以在其中使用Python代码。
Working on the Django tutorial at the moment and got this error when I tried:
python manage.py syncdb
Using 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
My settings.py file reads:
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '/Users/username/django_tutorial/mysite',
Anyone know the problem here? it seems like I am putting in the complete file directory, but the databases still couldn't be created? ):
You should use the path to the file, including the file name:
'NAME': '/Users/username/django_tutorial/mysite/data.sqlite',
You can also just give it the file name:
'NAME': 'data.sqlite',
In that case it will be created in the same directory than the one from which you run manage.py
.
The best practice is to pass an absolute path, but to ensure this absolute path is created relatively to the settings file. E.G:
import os
SETTINGS_DIR = os.path.dirname(os.path.abspath(__file__))
Then latter:
'NAME': os.path.join(SETTINGS_DIR, 'data.sqlite'),
Remember that the settings file is a Python file, and that you can use Python code in it.
这篇关于Django Newbie:python manage.py syncdb错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!