擅长:python、mysql、java
<p>如果希望每行数据都有可用的标题,请使用<code>DictCursor</code>。据我所知,最流行的MySQL、Oracle、Postgres libs都支持它</p>
<p>然后你可以这样做:</p>
<pre><code>conn = MySQLdb.connect(host,port,user,passwd,db)
cursor = van.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("SELECT * FROM table;")
# Avoid doing fetchall(). If your query resukt is big enough, it can even make your program run out of available memory of the system.
#rows=cursor.fetchall()
#Alternatively, iterate over the cursor itself, which is a generator
for row in cursor:
print row
</code></pre>
<p>引用:<a href="https://wiki.python.org/moin/Generators" rel="nofollow noreferrer">generator</a></p>