python - Pyodbc 查询仅返回一行

标签 python mysql sql pyodbc

我刚刚开始使用 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])

有什么想法吗?

谢谢!

最佳答案

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

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

关于python - Pyodbc 查询仅返回一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51102643/

相关文章:

python - 是否可以在 PyShark 中访问数据包的 hexdump?

python - 从 Linux 中的 acpi 事件执行 PyQt 应用程序

mysql - 我可以在我的 MySQL 数据库上将一些表与 InnoDB 引擎一起使用,而一些表与 MyIsam 一起使用吗?

SQL在查询结果中输出行号

javascript - Django:Bokeh.safely 不是函数

PHP 显示更新查询状态的消息框

mysql - 如何在 MYSQL 中通过另一列选择具有 MAX(列值)、PARTITION 的行?

mysql - 我怎样才能使用MySQL得到以下结果

SQL 通过 temptable 进行分组,如果其中一列值为 1,则获取新列值

企业中的 Python : Pros and cons