有没有办法将字典结果从adbapi查询返回给
MySQL?
[name: 'Bob',phone_number: '9123 4567']
默认返回元组.
['Bob','9123 4567']
对于简单的Python& MysqL我们可以使用MysqLdb.cursors.DictCursor.但如何使用扭曲的adbapi
UPD:我解决了,但我认为应该有更好的方法.我的解决方案:只需覆盖adbapi.ConnectionPool类的* _runInteraction *方法.
class MyAdbapiConnectionPool(adbapi.ConnectionPool): def _runInteraction(self,interaction,*args,**kw): conn = self.connectionFactory(self) trans = self.transactionFactory(self,conn) try: result = interaction(trans,**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,excTraceback