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