pyodbc查询只返回一行

vuktfyat  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(325)

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

有什么想法吗?
谢谢!

sdnqo3pr

sdnqo3pr1#

试试这个:

result = cursor.fetchall()

result = cursor.fetchmany()

参考文献:
https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchall.html
https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchmany.html

wribegjk

wribegjk2#

fetchone()获取一行。您可能希望改用fetchall()。例如:

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

相关问题