美文网首页
python的SQLite游标,fetchall报错object

python的SQLite游标,fetchall报错object

作者: Luy_ | 来源:发表于2021-05-21 10:56 被阅读0次

源码

 def exec_sql(self, sql):
        conn = self._get_conn()
        try:
            with conn as cur:
                cur.execute(sql)
                return cur.fetchall()
        except MySQLdb.ProgrammingError as e:
            LOG.error("execute sql ({0}) error {1}".format(sql, e))
            raise e
        except MySQLdb.OperationalError as e:
            conn = self._create_new_conn()
            raise e
        finally:
            self._put_conn(conn)

报错:AttributeError: 'Connection' object has no attribute 'execute'

类缺少方法fetchall,需要创建一个游标的实例,

from contextlib import closing
with closing(self.connectio.cursor()) as cur:

更简单的解决方法:删掉with

        try:
            cur.execute(sql)
            return cur.fetchall()

参考:https://stackoverflow.com/questions/16668623/sqlite-cursor-in-python-with-statement

相关文章

网友评论

      本文标题:python的SQLite游标,fetchall报错object

      本文链接:https://www.haomeiwen.com/subject/epaijltx.html