小菜鸡_弱弱 2023-05-26 15:51 采纳率: 84.1%
浏览 26
已结题

python获取返回值

python执行完之后返回的值要怎么样才能一个个取出来呢

img

返回值

img

  • 写回答

3条回答 默认 最新

  • CSDN专家-sinJack 2023-05-26 15:58
    关注

    for循环遍历查询结果

    import pymysql
    
    # 连接数据库
    conn = pymysql.connect(host='localhost', user='root', password='password', db='test', charset='utf8mb4')
    cursor = conn.cursor()
    
    # 查询数据
    sql = "SELECT * FROM students"
    cursor.execute(sql)
    results = cursor.fetchall()
    
    # 遍历查询结果并输出
    for row in results:
        for col in row:
            print(col, end='\t')
        print()
    
    # 关闭数据库连接
    cursor.close()
    conn.close()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月3日
  • 已采纳回答 5月26日
  • 创建了问题 5月26日