有沒有辦法把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