python - 创建 db.connection 自定义类包装器,不断收到“django.db.utils.InterfaceError: cursor already closed”
问题描述
我试图为“django.db.connection”制作自定义类包装器,但我不断收到“django.db.utils.InterfaceError:游标已关闭”。
如果没有自定义类包装器(复制自),则工作代码是这样的./manage.py shell
:
>>> from django.db import (connection as con)
>>> with con.cursor() as q:
... q.execute('select * from master.years a where a.years = %s', [str(2019)])
... f = [f.name for f in q.description]
... for b in q:
... print(dict(zip(f,b)))
我的包装webapp/mine/db.py
:
class query_00:
def __init__(self, db_con, db_sql, db_stmt = []):
self.db_con = db_con
self.db_sql = db_sql
self.fields = []
with db_con.cursor() as self.query:
self.query.execute(db_sql, db_stmt)
self.fields = [f.name for f in self.query.description]
# for entry in self.query:
# yield dict(zip(self.fields, entry))
# self.query = db_con.cursor()
# must return self, because
# AttributeError: 'NoneType' object has no attribute 'result'
def __enter__(self):
return self
# self.query.execute(self.db_sql, self.db_stmt)
# self.fields = [f.name for f in self.query.description]
# pass
def __exit__(self, exc_type, exc_value, traceback):
# is this necessary?
# self.db_con.close()
pass
def result(self):
for entry in self.query.fetchall():
yield dict(zip(self.fields, entry))
# not working
# for entry in self.query:
# yield dict(zip(self.fields, entry))
# not working
# yield dict(zip(self.fields, entry))
# pass
./manage.py shell
然后我通过输入试一下
from django.db import (connection as con)
from mine.db import query_00
# expected usage, attempt 1
q = query_00(con, 'SELECT * FROM master.years a where a.year = %s', [str(2019),])
for b in q.result():
print(b)
# expected usage, attempt 2
with query_00(con, 'SELECT * FROM master.years a where a.year = %s', [str(2019),]) as q:
for b in q.result():
print(b)
python-3,django-2,postgresql-9
(对不起我的英语不好)
解决方案
所以,在阅读了文档之后,我意识到连接在我的 init 方法中被关闭了。
class query_00:
def __init__(self, db_con, db_sql, db_stmt = []):
self.db_con = db_con
self.db_sql = db_sql
self.fields = []
with db_con.cursor() as self.query:
self.query.execute(db_sql, db_stmt)
self.fields = [f.name for f in self.query.description]
# the connection only happend inside "with:" block
# the connection, was already closed here (after with:)
所以,我只需要将 cursor() 保留在变量上
class query_01:
def __init__(self, db_sql, db_stmt):
self.db_sql = db_sql
self.db_stmt = db_stmt
self.query = connection.cursor()
...
换query.execute()
一种方法
...
def execute(self):
self.query.execute(self.db_sql, self.db_stmt)
self.fields = [f.name for f in self.query.description]
...
结果()
...
def result(self):
self.execute()
for entry in self.query:
yield self.keyvalue(entry)
...
然后我的课正在工作
with query_00('SELECT * FROM master.years a where a.year = %s', [str(2019),]) as q:
for b in q.result():
print(b)