2011-10-06 56 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能夠與數據庫交談。 –

相關問題