在IPython笔记本实例中通过SQLAlchemy进行连接时,我收到OperationalError: (OperationalError) database is locked
错误,我不确定为什么。
我已经使用 SQLAlchemy 和 Declarative Base 语法为 SQLite 数据库编写了一个 Python 接口。我将数据库模型导入 IPython 笔记本以浏览数据。今天早上效果很好。这是代码:
from psf_database_interface import session, PSFTable
query = session.query(PSFTable).first()
但是今天下午,在我关闭笔记本电脑并运行IPython(它可以很好地重新启动服务器)后,我开始收到此错误。这很奇怪,因为我仍然可以从 SQLite3 命令行工具打开数据库并查询数据。我不希望任何其他进程连接到此数据库并在数据库上运行fuser
确认这一点。我的应用程序没有使用任何并发进程(在我编写的代码中,如果某些东西埋藏在SQLAlchemy或IPython中,IDK),即使它只是在执行读取操作,SQLite确实支持并发。
我尝试重新启动IPython内核以及杀死并重新启动IPython笔记本服务器。我尝试创建数据库的备份并用备份替换数据库,如下所示:https://stackoverflow.com/a/2741015/1216837。最后,出于绝望,我尝试添加以下内容,看看是否可以以某种方式清除卡在会话中的某些内容:
print session.is_active
session.flush()
session.close()
session.close_all()
print session.is_active
返回True
和True
.有什么想法吗?
更新:我可以运行导致 python 文件错误的代码片段,没有任何问题,该问题仅发生在 IPython 中。
我遇到了同样的问题。我可以运行python脚本,但IPython引发了以下异常。
您需要检查fuser
没有使用它的进程。但是,如果找不到任何内容,并且命令历史记录对您来说并不重要,则可以使用以下解决方法。
当我删除/home/my_user/.ipython/profile_default/history.sqlite
文件时,我可以启动IPython。正如我上面提到的,历史是空的。
$ ipython
[TerminalIPythonApp] ERROR | Failed to create history session in /home/my_user/.ipython/profile_default/history.sqlite. History will not be saved.
Traceback (most recent call last):
File "/home/esadrfa/libs/anaconda3/lib/python3.6/site-packages/IPython/core/history.py", line 543, in __init__
self.new_session()
File "<decorator-gen-22>", line 2, in new_session
File "/home/esadrfa/libs/anaconda3/lib/python3.6/site-packages/IPython/core/history.py", line 58, in needs_sqlite
return f(self, *a, **kw)
File "/home/esadrfa/libs/anaconda3/lib/python3.6/site-packages/IPython/core/history.py", line 570, in new_session
self.session_number = cur.lastrowid
sqlite3.OperationalError: database is locked
[TerminalIPythonApp] ERROR | Failed to open SQLite history :memory: (database is locked).