我在 https://www.pythonanywhere.com 上使用 flask 和 sqlite3 .在我自己的机器上,当我测试应用程序时,我不需要指定数据库的目录,例如 db = sqlite3.connect("database.db")
它完美地工作。在 pythonanywhere 上,我需要将其更改为 db = sqlite3.connect("/path/to/database.db")
因为当我不改变时,我会得到这个错误:
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
在/var/log/<application>.pythonanywhere.com.error.log
,我得到:
OperationalError: no such table: <table-name>
我发现一个空的 database.db
在我位于 pythonanywhere 的主文件夹中(这意味着应用程序创建了它,对吗?)和 database.db
我创建的是在项目文件夹中
有什么方法可以指定目录以使其在我的机器和 pythonanywhere 上都能完美运行而无需更改路径?
最佳答案
在你的 db
文件中,试试这个方法:
from os import path
ROOT = path.dirname(path.realpath(__file__))
...
db = sqlite3.connect(path.join(ROOT, "database.db"))
...
不是直接指出路径,ROOT
始终指向包含 db
的文件的目录,那么您应该为您的数据库获取正确的位置.
关于Python-Flask - 如何在不指定目录的情况下访问 pythonanywhere.com 上的 sqlite database.db?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34510196/