源碼
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)
報(bào)錯(cuò):AttributeError: 'Connection' object has no attribute 'execute'
類缺少方法fetchall,需要?jiǎng)?chuàng)建一個(gè)游標(biāo)的實(shí)例,
from contextlib import closing
with closing(self.connectio.cursor()) as cur:
更簡(jiǎn)單的解決方法:刪掉with
try:
cur.execute(sql)
return cur.fetchall()
參考:https://stackoverflow.com/questions/16668623/sqlite-cursor-in-python-with-statement