我创建了一个Django项目和Django应用程序。我希望Django为我的项目创建SQLite数据库。我运行manage.py syncdb
但我收到此错误:
Traceback (most recent call last):
File "C:\Users\home\Google ─шёъ\Python\testsite\manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 285, in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 415, in handle
return self.handle_noargs(**options)
File "C:\Python27\lib\site-packages\django\core\management\commands\syncdb.py", line 57, in handle_noargs
cursor = connection.cursor()
File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 160, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 132, in _cursor
self.ensure_connection()
File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 127, in ensure_connection
self.connect()
File "C:\Python27\lib\site-packages\django\db\utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 127, in ensure_connection
self.connect()
File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 115, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line 347, in get_new_connection
conn = Database.connect(**conn_params)
django.db.utils.OperationalError: unable to open database file
出了什么问题?
以下是settings.py
的数据库设置:
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
答案 0 :(得分:1)
我可以给你的第一个(一般的)建议就是用这个开始所有你的Python文件:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
在处理外国符号时,你会给自己带来很多麻烦。
现在,如果这没有帮助,也许检查您是否对尝试创建数据库文件的目录(BASE_DIR
)具有写入权限。
答案 1 :(得分:1)
当您使用sqlite3作为数据库时,这很常见:https://code.djangoproject.com/wiki/NewbieMistakes#DjangosaysUnabletoOpenDatabaseFilewhenusingSQLite3
确保文件db.sqlite3
和包含它的目录(在本例中为BASE_DIR
)具有正确的权限设置。
通常(取决于操作系统)
chown -R www-data:www-data path_to_basedir
诀窍
答案 2 :(得分:1)
os.path.dirname(path)
返回路径名的目录。这假设路径作为输入传递,而不是您当前正在执行的文件。
来自docs:
os.path.dirname(path)
返回路径名路径的目录名称。这是通过将路径传递给函数split()返回的对中的第一个元素。
您正在使用BASE_DIR = os.path.dirname(os.path.dirname(__file__))
,这将返回一个空字符串,因此您的NAME
值将不正确。
在python提示符上检查以下演示(从this question获取提示):
>>> import os
>>> __file__ = "test.py" #Don't use this line if running from within a file
>>> print os.path.dirname(__file__)
>>> print os.path.abspath(__file__)
/home/user/Desktop/test/test.py
>>> print os.path.basename(__file__)
test.py
>>> print os.getcwd()
/home/user/Desktop/test
>>> BASE_DIR = os.path.dirname(os.path.dirname(__file__))
>>> NAME = os.path.join(BASE_DIR, 'db.sqlite3')
>>> print BASE_DIR, len(BASE_DIR)
0
>>> print NAME, len(NAME)
db.sqlite3 10
因此,您需要重新定义BASE_NAME
的值,并可以在设置文件中使用以下内容:
BASE_DIR = os.getcwd()
# Or alternatively, use below commented line
# BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
答案 3 :(得分:1)
**确保路径中没有特殊字符,例如“éè”或“(”。**
Django says "Unable to Open Database File" when using SQLite3