Question

I am trying to append certain values row[9:11] to my query results IF rows0-8 are already in existence (to avoid redundancy and move from many rows to one row, many columns). The query executes fine, and if I remove the "else" clause, it works fine. However, I'm not getting it to print the rows that already exist with the additional values (and I know these rows exist)... any ideas what I'm missing?

cursor.execute(query, cruise6_input=cruise6_input)
output=""
checkID=""
for row in cursor.fetchall():

    if row[0] != checkID:
        if output != "":
            print output, "this is a test"

        checkID=row[0]
        output=row[0:11]


    else:
        output=output + (row[8:11])

this printed all of the rows of original output and the "this is a test"...

Was it helpful?

Solution

My nesting was off. Instead of:

if row[0] != checkID:
    if output != "":
        print output, "this is a test"

    checkID=row[0]
    output=row[0:11]


else:
    output=output + (row[8:11])

I had:

  if row[0] != checkID:
        if output != "":
            print output, "this is a test"

            checkID=row[0]
            output=row[0:11]


    else:
        output=output + (row[8:11])

Silly typos. Thanks all!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top