所以现在我有一个返回大约 20 行的查询。 我稍后确实需要这些
cur.execute(query)
for rows in cur.fetchall():
print(rows)
cur.close()
conn.close()
我如何编写 for 循环以仅写入 1-3 范围内的行?
最佳答案
使用切片表示法从行中获取前三项。 http://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/
for row in cur.fetchall()[:3]:
print row
关于python - 只想在 python 中显示 mySQL 结果的 3 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27016452/