gpt4 book ai didi

python - 如何使用带有 for 或 while 循环的 MySQLdb SELECT

转载 作者:行者123 更新时间:2023-11-28 19:41:00 25 4
gpt4 key购买 nike

import _mysql as mysql
db=mysql.connect('localhost','username','password','database')


db.query("""select * from news""")

result = db.store_result()


print result.num_rows()#two records

#how to loop? without cursor

print result.fetch_row()

最佳答案

你可以试试这个:

while True:
record = result.fetch_row()
if not record: break
print record

我第二@Ignacionote of caution反对使用 _mysql。切换到import MySQLdb

关于python - 如何使用带有 for 或 while 循环的 MySQLdb SELECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3633550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com