我想在 django 中使用 sqlite 进行开发,并进行以下设置:
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'D:\database.db', # 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.
}
但是当我尝试执行syncdb时,我得到以下信息:
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
438, in execute_manager
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 191,
in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 220,
in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 351,
in handle
return self.handle_noargs(**options)
File "C:\Python27\lib\site-packages\django\core\management\commands\syncdb.py"
, line 56, in handle_noargs
cursor = connection.cursor()
File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 250,
in cursor
cursor = self.make_debug_cursor(self._cursor())
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line
207, in _cursor
self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file
知道发生了什么吗? 请注意,我多次尝试更改数据库名称(位置),但没有成功...
最佳答案
使用正斜杠或 r''
strings首先。
'default': {
# [... snip ...]
'NAME': 'D:/database.db',
# (or...) 'NAME': r'D:\database.db',
# [... snip ...]
}
此外,您需要确保 Sqlite 文件所在的整个目录可写(在本例中为 D:/
),因为 Sqlite 接下来需要写入其日志文件到数据库文件。
关于python - 在 Windows 中使用 Django Sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7722118/