有没有办法把adbapi查询的字典结果返回到MySQL?在[name: 'Bob', phone_number: '9123 4567']
默认返回元组。在
^{pr2}$
对于简单的Python&MySQL,我们可以使用MySQLdb.cursors.dictcursors。但是如何与扭曲的adbapi一起使用呢
UPD:我解决了,但我认为应该有更好的方法。我的解决方案:只需重写adbapi.ConnectionPool课程。在class MyAdbapiConnectionPool(adbapi.ConnectionPool):
def _runInteraction(self, interaction, *args, **kw):
conn = self.connectionFactory(self)
trans = self.transactionFactory(self, conn)
try:
result = interaction(trans, *args, **kw)
if(result and isinstance(result[0], (list, tuple))):
colnames = [c[0] for c in trans._cursor.description]
result = [dict(zip(colnames, item)) for item in result]
trans.close()
conn.commit()
return result
except:
excType, excValue, excTraceback = sys.exc_info()
try:
conn.rollback()
except:
log.err(None, 'Rollback failed')
raise excType, excValue, excTraceback