Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Flask-如何在不指定目录的情况下访问pythonanywhere.com上的sqlite database.db?_Python_Database_Flask_Sqlite_Pythonanywhere - Fatal编程技术网

Python Flask-如何在不指定目录的情况下访问pythonanywhere.com上的sqlite database.db?

Python Flask-如何在不指定目录的情况下访问pythonanywhere.com上的sqlite database.db?,python,database,flask,sqlite,pythonanywhere,Python,Database,Flask,Sqlite,Pythonanywhere,我正在使用flask和sqlite3。在我自己的机器中,当我测试应用程序时,我不需要指定数据库的目录,例如db=sqlite3.connect(“database.db”),它工作得很好。在PythonyWhere上时,我需要将其更改为db=sqlite3.connect(“/path/to/database.db”),因为当我不更改时,会出现以下错误: Internal Server Error The server encountered an internal error and was

我正在使用flask和sqlite3。在我自己的机器中,当我测试应用程序时,我不需要指定数据库的目录,例如
db=sqlite3.connect(“database.db”)
,它工作得很好。在PythonyWhere上时,我需要将其更改为
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/.pythonanywhere.com.error.log
中,我得到:

OperationalError: no such table: <table-name>
操作错误:没有这样的表:
我在pythonanywhere的主文件夹中发现了一个空的
database.db
(这意味着应用程序创建了它,对吗?),我创建的
database.db
位于projects文件夹中


是否有任何方法可以指定目录,使其在我的机器和pythonanywhere上都能正常工作而不更改路径?

db
文件中,尝试以下方法:

from os import path

ROOT = path.dirname(path.realpath(__file__))

...
db = sqlite3.connect(path.join(ROOT, "database.db"))
...

与直接指出路径不同,
根目录始终指向包含
db
的文件目录,然后您应该找到数据库的正确位置。

谢谢!它现在在我的机器和蟒蛇世界里都运行得很好!我有完全相同的问题…@不,你试过上帝的答案吗。j?是的。它对我有用。非常感谢你们两位。@nos谢谢他更多:D