python - 在 Windows 中使用 Django Sqlite

标签 python django sqlite

我想在 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/

相关文章:

python - PIL 和 py2exe 的问题

python - 使用 python 进行 Borda 计数?

javascript - 带数据返回的 Python CGI

Apache 上的 Django - 防止 504 网关超时

iphone - 从 sqlite 数据库中查找列中的最大值

sqlite - 从sqlite导入数据到postgres : easily convert AUTOINCREMENT

c# - 在 Windows 中的两个不同应用程序之间共享 sqlite 数据库

数组中的 Python Numba 值

python - 在 Django View 中高效地导入模块

python - 如何在 Django 管理更改表单中显示相关表的内容?