Mike - SMT
Mike - SMT

Reputation: 15226

for loop and if statement interaction is not working as expected?

I am not sure how my code is skipping the first item in my list...

I have tried checking if value is the same as first item in list and tried to check index zero as well but both skip the first value.

I have been looking at this for a while. Am I missing something obvious?

Example using enumerate and checking the index.

def threaded_query(self):
    for ndex, query_key in enumerate(['Acct #', 'Results', 'Final Results', 'IPAccts']):
        if ndex == 0:
            write_to_table = pow_query_dict[query_key].format(self.login, self.value)
        else:
            write_to_table = pow_query_dict[query_key].format(self.login)

            conn = pyodbc.connect(driver='{ODBC Driver 17 for SQL Server}', host='myserver',
                                  database='myDB', trusted_connection='yes')
            with conn:
                try:
                    conn.autocommit = True
                    cursor = conn.cursor()
                    cursor.execute(write_to_table)
                    print('Committed {} data to SQL Server tables.'.format(query_key))
                except BaseException as e:
                    print('Query failed with Exception: {}'.format(e))

Example checking for the exact value of first item in list:

def threaded_query(self):
    for query_key in ['Acct #', 'Results', 'Final Results', 'IPAccts']:
        if query_key == 'Acct #':
            write_to_table = pow_query_dict[query_key].format(self.login, self.value)
        else:
            write_to_table = pow_query_dict[query_key].format(self.login)

            ...

Both Results:

Committed Results data to SQL Server tables.
Committed Final Results data to SQL Server tables.
Committed IPAccts data to SQL Server tables.

As you can see in the results it appears to completely skip the Acct # in the if statement.

Upvotes: 0

Views: 121

Answers (2)

snydez
snydez

Reputation: 29

Well, already posted an answer while I was writing this hah:

But your with needs to be outside the else block.

Upvotes: 0

Simon Notley
Simon Notley

Reputation: 2136

Your with block is inside the else. It should be

def threaded_query(self):
    for ndex, query_key in enumerate(['Acct #', 'Results', 'Final Results', 'IPAccts']):
        if ndex == 0:
            write_to_table = pow_query_dict[query_key].format(self.login, self.value)
        else:
            write_to_table = pow_query_dict[query_key].format(self.login)

        conn = pyodbc.connect(driver='{ODBC Driver 17 for SQL Server}', host='myserver',
                                  database='myDB', trusted_connection='yes')
        with conn:
            try:
                conn.autocommit = True
                cursor = conn.cursor()
                cursor.execute(write_to_table)
                print('Committed {} data to SQL Server tables.'.format(query_key))
            except BaseException as e:
                print('Query failed with Exception: {}'.format(e))

Upvotes: 5

Related Questions