Python sqlalchemy.exc.OperationalError: (OperationalError) 无法打开数据库文件 无 无
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18208492/
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
sqlalchemy.exc.OperationalError: (OperationalError) unable to open database file None None
提问by user2677756
I am running a program from another person who are inconvenience ask for help from. The program is a website. Server end is written by python and flask (module, http://flask.pocoo.org/). The program has been successfully run on the server. What I need to do is modify something on it. Since the production server is not allowed for test, I tested it in development server locally via flask. However, I could not run even the original program. Below is from python.
我正在运行另一个不便寻求帮助的人的程序。该程序是一个网站。服务器端由python和flask编写(模块,http://flask.pocoo.org/)。该程序已在服务器上成功运行。我需要做的是在上面修改一些东西。由于生产服务器不允许进行测试,我通过flask在本地的开发服务器上进行了测试。但是,我什至无法运行原始程序。以下来自python。
(venv)kevin@ubuntu:~/python/public_html$ python index.wsgi
Traceback (most recent call last): File "index.wsgi", line 6, in from app import app as application
回溯(最近一次调用最后一次):文件“index.wsgi”,第 6 行,在 from app import app as application
File "/home/kevin/python/public_html/app.py", line 27, in <module>
app = create_app()
File "/home/kevin/python/public_html/app.py", line 12, in create_app
database.init_db()
File "/home/kevin/python/public_html/database.py", line 24, in init_db
Base.metadata.create_all(engine)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/schema.py", line 2793, in create_all
tables=tables)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1478, in _run_visitor
with self._optional_conn_ctx_manager(connection) as conn:
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1471, in _optional_conn_ctx_manager
with self.contextual_connect() as conn:
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1661, in contextual_connect
self.pool.connect(),
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 272, in connect
return _ConnectionFairy(self).checkout()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 425, in __init__
rec = self._connection_record = pool._do_get()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 857, in _do_get
return self._create_connection()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 225, in _create_connection
return _ConnectionRecord(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 318, in __init__
self.connection = self.__connect()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 368, in __connect
connection = self.__pool._creator()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/strategies.py", line 80, in connect
return dialect.connect(*cargs, **cparams)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 283, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.OperationalError: (OperationalError) unable to open database file None None
In the config.py file
在 config.py 文件中
LOGFILE = '/tmp/ate.log' DEBUG = True TESTING = True THREADED = True DATABASE_URI = 'sqlite:////tmp/ate.db' SECRET_KEY = os.urandom(24)
LOGFILE = '/tmp/ate.log' DEBUG = True TESTING = True THREADED = True DATABASE_URI = 'sqlite:////tmp/ate.db' SECRET_KEY = os.urandom(24)
Hence, I created a folder called "tmp" under my user and an empty file called "ate.db". Then, ran it again. It said
因此,我在我的用户下创建了一个名为“tmp”的文件夹和一个名为“ate.db”的空文件。然后,再次运行它。它说
IOError: [Errno 2] No such file or directory: '/home/kevin/log/ate.log'
IOError: [Errno 2] 没有这样的文件或目录:'/home/kevin/log/ate.log'
Then, I created the log folder and the log file. Run it, but nothing happened like
然后,我创建了日志文件夹和日志文件。运行它,但什么也没发生
(venv)kevin@ubuntu:~/python/public_html$ python index.wsgi (venv)kevin@ubuntu:~/python/public_html$ python index.wsgi (venv)kevin@ubuntu:~/python/public_html$
(venv)kevin@ubuntu:~/python/public_html$ python index.wsgi (venv)kevin@ubuntu:~/python/public_html$ python index.wsgi (venv)kevin@ubuntu:~/python/public_html$
If it is successful, the website should be available on http://127.0.0.1:5000
/. However, it did not work. Does anybody know why and how to solve it? The codes should be fine since it is now available online. The problem should be a local problem. Thank you so much for your help.
如果成功,该网站应该在http://127.0.0.1:5000
/上可用。但是,它不起作用。有谁知道为什么以及如何解决它?代码应该没问题,因为它现在可以在线获得。问题应该是局部问题。非常感谢你的帮助。
The code of where the program is stuck
程序卡在哪里的代码
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
engine = None
db_session = None
Base = declarative_base()
def init_engine(uri, **kwards):
global engine
engine = create_engine(uri, **kwards)
return engine
def init_db():
global db_session
db_session = scoped_session(sessionmaker(bind=engine))
# import all modules here that might define models so that
# they will be registered properly on the metadata. Otherwise
# you will have to import them first before calling init_db()
import models
Base.metadata.create_all(engine)
回答by Eric Smith
I think I've seen errors like this where file permissions were wrong for the .db file or its parent directory. You might make sure that the process trying to access the database can do so by appropriate use of chown
or chmod
.
我想我见过这样的错误,其中 .db 文件或其父目录的文件权限错误。您可以通过适当使用chown
或来确保尝试访问数据库的进程可以这样做 chmod
。
This is specifically about Django, but maybe still relevant: https://serverfault.com/questions/57596/why-do-i-get-sqlite-error-unable-to-open-database-file
这特别是关于 Django,但可能仍然相关:https: //serverfault.com/questions/57596/why-do-i-get-sqlite-error-unable-to-open-database-file
回答by Finch_Powers
I had this issue with sqlite. The process trying to open the database file needs to have write access to the directory as it creates temporary/lock files.
我在使用 sqlite 时遇到了这个问题。尝试打开数据库文件的进程需要对目录具有写访问权限,因为它会创建临时/锁定文件。
The following structure worked for me to allow www-data to use the database.
以下结构对我有用,以允许 www-data 使用数据库。
%> ls -l
drwxrwxr-x 2 fmlheureux www-data 4096 Feb 17 13:24 database-dir
%> ls -l database-dir/
-rw-rw-r-- 1 fmlheureux www-data 40960 Feb 17 13:28 database.sqlite
回答by Hannu Hjelm
My database URI started rocking after adding one dot in between ////
. Working on windows 7. I had directory and db-file created prior to calling this.
我的数据库 URI 在////
. 在 Windows 7 上工作。在调用它之前我已经创建了目录和 db 文件。
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///./dbdir/test.db'
回答by Nico
I just met this same problem and found that I make a stupid circular reference .
我刚刚遇到了同样的问题,发现我做了一个愚蠢的循环引用。
./data_model.py
./data_model.py
from flask.ext.sqlalchemy import SQLAlchemy from api.src.app import app app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////database/user.db') db = SQLAlchemy(app)
from flask.ext.sqlalchemy import SQLAlchemy from api.src.app import app app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////database/user.db') db = SQLAlchemy(app)
./app.py
./app.py
... from api.src.data_model import db db.init_app(app)
... from api.src.data_model import db db.init_app(app)
Then I removed the app.py/db and it works.
然后我删除了 app.py/db 并且它可以工作。
回答by britodfbr
Replace:
代替:
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////dbdir/test.db'
With:
和:
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///dbdir/test.db'
回答by Jayson Kaeze
finally figured it out, had help tho
终于想通了,有帮助寿
import os
file_path = os.path.abspath(os.getcwd())+"\database.db"
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+file_path
db = SQLAlchemy(app)
回答by Igor Micev
You're not managing to find the path to the database from your current level. What you need to do is the following:
您无法从当前级别找到数据库的路径。您需要做的是:
DATABASE_URI = 'sqlite:///../tmp/ate.db'
That means go up to the root level ..
and then navigate down to the database (the relative path is /tmp/ate.db
in this case).
这意味着上升到根级别..
,然后向下导航到数据库(/tmp/ate.db
在这种情况下是相对路径)。