如何在 WINdows 7 上使用带有 DJANGO 的 SQLITE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1805852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:29:50  来源:igfitidea点击:

How can I use SQLITE with DJANGO on WIndows 7

pythonwindowsdjangosqlitewindows-7

提问by REA_ANDREW

I am following the tutorial on the DJango site, which I previsouly did using Windows XP and everything went fine, but on Windows 7 I get the following error:

我正在关注 DJango 站点上的教程,我以前使用 Windows XP 完成了该教程并且一切正常,但是在 Windows 7 上我收到以下错误:

sqlite3.OperationalError: unable to open database file

I use the following:

我使用以下内容:

python manage.py sql Blog

Does any one have any ideas what might be wrong. The database file is located in C:\Software\Sqlite\Databases\Blog.db

有没有人有任何想法可能是错误的。数据库文件位于C:\Software\Sqlite\Databases\Blog.db

And the relative settings.py or section of is simply:

而相对 settings.py 或部分只是:

DATABASE_ENGINE = 'sqlite3'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'C:\Software\Sqlite\databases\blog.db'             # Or path to database file if using sqlite3.
DATABASE_USER = ''             # Not used with sqlite3.
DATABASE_PASSWORD = ''         # Not used with sqlite3.
DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not used with

I have also for testing purposes added everyone with full permissions.

我还出于测试目的添加了具有完全权限的所有人。

But as I say I get the following error:

但正如我所说,我收到以下错误:

sqlite3.OperationalError: unable to open database file

Any help is appreciated,

任何帮助表示赞赏,

Andrew

安德鲁

采纳答案by ikkebr

I don't think you can use a full windows path to access your sqlite database. I've run some tests here and the only way I could use a sqlite3 database on django not in the same directory of the project was using DATABASE_NAME = '../anotherfolder/db.db' (this was located at c:\anotherfolder\ and project was located at c:\mydjangoproject)

我认为您不能使用完整的 Windows 路径来访问您的 sqlite 数据库。我在这里运行了一些测试,我可以在不在项目的同一目录中的 django 上使用 sqlite3 数据库的唯一方法是使用 DATABASE_NAME = '../anotherfolder/db.db'(它位于 c:\anotherfolder \ 和项目位于 c:\mydjangoproject)

回答by Mark Byers

I know this question has already an accepted answer, but I think you missed something. You should use raw strings when your strings contain backslashes:

我知道这个问题已经得到了公认的答案,但我认为你错过了一些东西。当您的字符串包含反斜杠时,您应该使用原始字符串:

DATABASE_NAME = r'C:\Software\Sqlite\databases\blog.db' 

This is what happens if you don't use a raw string:

如果您不使用原始字符串,则会发生以下情况:

>>> print 'C:\Software\Sqlite\databases\blog.db'
C:\Software\Sqlite\databaselog.db

回答by Jeduan Cornejo

Have a look at the permissions of the folder. Can your regular user edit files there?

查看文件夹的权限。您的普通用户可以在那里编辑文件吗?

回答by Zerqent

A guess is that the file is actually not in that dir. If you have UAC enabled windows 7 will make it look like the file is at that location (it will show up in windows explorer). However the file is really stored in c:\users\yourusername\AppData\Local\VirtualStore\Software\Sqlite\databases or a similar location.

猜测是该文件实际上不在该目录中。如果您启用了 UAC,Windows 7 将使文件看起来像是在该位置(它将显示在 Windows 资源管理器中)。然而,该文件实际上存储在 c:\users\yourusername\AppData\Local\VirtualStore\Software\Sqlite\databases 或类似位置。

回答by sami

Reverse the slashes in DATABASE_NAME: from C:\Software\Sqlite\databases\blog.dbto C:/Software/Sqlite/databases/blog.db

反转 DATABASE_NAME 中的斜线:从C:\Software\Sqlite\databases\blog.dbC:/Software/Sqlite/databases/blog.db

I am using django 1.4 on Win7 and this was the solution for me - the file doesn't need to exist already

我在 Win7 上使用 django 1.4,这是我的解决方案 - 该文件不需要已经存在

回答by AASIO

Reversing the slashes in the database name solved the problem for me.

反转数据库名称中的斜杠为我解决了这个问题。