我正在尝试从SQL查询中获取输出,并将其写入变量。这是使用Redshift DB
cur.execute(sql.SQL("""select prod_name,prod_category,count(*) from sales limit 5""")
sql_output = cur.fetchall()
上面提取5条记录并将其存储到sql_output。现在,我尝试将其写入PowerPoint幻灯片中的表格,如下所示:
name_1 = sql_output[0][0]
category_1 = sql_output[0][1]
count_1 = sql_output[0][3]
name_2 = sql_output[1][0]
category_2 = sql_output[1][1]
count_2 = sql_output[1][3]
name_3 = sql_output[2][0]
category_3 = sql_output[2][1]
count_3 = sql_output[2][3]
name_4 = sql_output[3][0]
category_4 = sql_output[3][1]
count_4 = sql_output[3][3]
name_5 = sql_output[4][0]
category_5 = sql_output[4][1]
count_5 = sql_output[4][3]
if len(sql_output) = []:
table.cell(1, 0).text = str('NA')
table.cell(1, 1).text = str('NA')
table.cell(1, 2).text = str('NA')
elif:
table.cell(1, 0).text = name_1
table.cell(1, 1).text = str(category_1)
table.cell(1, 2).text = str(count_1)
table.cell(2, 0).text = name_2
table.cell(2, 1).text = str(category_2)
table.cell(2, 2).text = str(count_2)
table.cell(3, 0).text = name_3
table.cell(3, 1).text = str(category_3)
table.cell(3, 2).text = str(count_3)
table.cell(4, 0).text = name_4
table.cell(4, 1).text = str(category_4)
table.cell(4, 2).text = str(count_4)
table.cell(5, 0).text = name_5
table.cell(5, 1).text = str(category_5)
table.cell(5, 2).text = str(count_5)
以下内容工作正常,但是输出为null,则以上内容失败并出现错误:
IndexError: list index out of range
答案 0 :(得分:0)
尝试一下
for index, row in enumerate(sql_output):
table.cell(index+1, 0).text = row[0]
table.cell(index+1, 1).text = str(row[1])
table.cell(index+1, 2).text = str(row[3])