【问题标题】:Pyodbc query just returns one rowPyodbc 查询只返回一行
【发布时间】:2018-06-29 13:15:02
【问题描述】:

我刚开始使用 Pyodbc,我正在尝试运行一个我知道使用 Sequel Pro 可以完美运行的查询。查询必须返回几行,但是,我只能得到一个。

这是我的查询:

with connection.cursor() as cursor:
        # Read a single record
        sql = "select `T4`.`id`, `T4`.`r_id`, `structure`.`l2_id`, `structure`.`l2_name` from (select `T3`.`id`, `T3`.`r_id`, `item`.`L6_ID` from (select `T2`.`id`, `report`.`r_id` from (select `T1`.`id` from (select `entity`.`id` from `entity` where `entity`.`id` = %s) as `T1` inner join `coverage` on `T1`.`id` = `coverage`.`id`) as `T2` inner join `report` on `T2`.`id` = `report`.`id`) as `T3` inner join `item` on `T3`.`r_id` = `item`.`r_id`) as `T4` inner join `structure` on `T4`.`l6_id` = `structure`.`l6_id`"

 cursor.execute(sql, ('id1',))
 result = cursor.fetchone()
 for row in result:
   print(row[0])

有什么想法吗?

谢谢!

【问题讨论】:

    标签: python mysql sql pyodbc


    【解决方案1】:

    fetchone() 获取单行。您可能想改用 fetchall() 。例如:

    rows = cursor.fetchall()
    for row in rows:
        print(row[0])
    

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 2012-11-19
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-11
      • 1970-01-01
      相关资源
      最近更新 更多