2011-10-06 147 views
36

在Django中设置服务器时出现此错误。这是sqlite3这意味着它应该创建.db文件,但它似乎并没有这样做。我已经规定SQLite作为后端和一个绝对的文件路径来放置它,但没有运气。sqlite3.OperationalError:无法打开数据库文件

这是一个错误还是我做错了什么? (只是在想,是在Ubuntu不同指定的绝对路径?)

这里是我的settings.py文件的开头:

# Django settings for OmniCloud project. 

DEBUG = True 
TEMPLATE_DEBUG = DEBUG 

ADMINS = (
# ('Your Name', '[email protected]'), 
) 

MANAGERS = ADMINS 

DATABASES = { 
'default': { 
    'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 
    'NAME': '~/Harold-Server/OmniCloud.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. 
} 
} 
+2

可惜的是,原来的错误信息并没有覆盖导致错误的文件名,这可能会有所帮助。 – Hartmut

回答

60

Django NewbieMistakes

PROBLEM You're using SQLite3, your DATABASE_NAME is set to the database file's full path, the database file is writeable by Apache, but you still get the above error.

SOLUTION Make sure Apache can also write to the parent directory of the database. SQLite needs to be able to write to this directory.

Make sure each folder of your database file's full path does not start with number, eg. /www/4myweb/db (observed on Windows 2000).

If DATABASE_NAME is set to something like '/Users/yourname/Sites/mydjangoproject/db/db', make sure you've created the 'db' directory first.

Make sure your /tmp directory is world-writable (an unlikely cause as other thing on your system will also not work). ls /tmp -ald should produce drwxrwxrwt ....

Make sure the path to the database specified in settings.py is a full path.

Also make sure the file is present where you expect it to be.

+0

如何通过Linux中的命令行更改Apache的权限? – Chris

+0

您不会更改apache的权限,您可以更改文件和文件夹的权限,以便Apache可以在正确的位置读取和写入。这里是chmod http://catcode.com/teachmod/的指南,这是你在linux中更改权限的方式。虽然它应该只是chmod + rw文件夹名称 – John

+2

好吧,我尝试过但仍然得到相同的错误:( – Chris

7

你没有指定了绝对路径 - 您使用了一个快捷方式,~,这可能无法在此上下文中使用。改为使用/home/yourusername/Harold-Server/OmniCloud.db

+0

我试过这个,但仍然得到了同样的错误。 – Chris

+0

将用户名作为服务器的名称,或者因为我以root身份登录,root? – Chris

+0

不要以超级用户身份登录。为Django站点设置用户,或者使用Apache用户(通常是www-data)。 –

18

我面临完全相同的问题。这是我的设置工作。

'ENGINE': 'django.db.backends.sqlite3', 
'NAME': '/home/path/to/your/db/data.sqlite3' 

在sqlite3的情况下,其他设置将相同/默认。
而你需要创建data.sqlite3。

0

使用此类型它适用于我。 Windows 7的使用Python 2.7和1.5的Django

'ENGINE': 'django.db.backends.sqlite3', 
'NAME': 'C:\\tool\\mysite\\data.db', 

希望它的作品...

4

您需要使用的,而不是~/完整路径。

在你的情况,像/home/harold/Harold-Server/OmniCloud.db

2

在我的情况下,sqlite数据库文件db.sqlite3存储在Apache的DocumentRoot。所以,即便是设置了下列权限后,没有工作:

sudo chown www-data:www-data /path/to/db-folder 
sudo chown www-data:www-data /path/to/db-folder/sqlite-db.db 

最后,当我提出db.sqlite3到新创建的文件夹dbfolderDocumentRoot下,给上述权限,和它的工作。

+0

这对我来说完全可以从根用户那里得到解决 - 使用烧瓶应用程序从项目目录内连接到数据库。工作一个魅力 - 只需要让Apache能够与数据库交谈。 –

相关问题