0

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.

asked Nov 19, 2019 at 16:10
2
  • 3
    There seems to be an extra indentation after last write_to_table = pow.. line ? Commented Nov 19, 2019 at 16:13
  • @SeljukGülcan ah yep that's it. I knew it was something obvious... That's what happens when you stare at your code for way to long. Probably should have left and come back. Its annoying that for some reason when you work in PyCharm it changes indention from time to time when pasting stuff. Commented Nov 19, 2019 at 16:14

2 Answers 2

5

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))
answered Nov 19, 2019 at 16:14
Sign up to request clarification or add additional context in comments.

3 Comments

...and in the time I wrote this it's already solved in the comments!
I knew it had to be something obvious I was not seeing. Been working on my program for wayyy to long today.
@Mike-SMT Ah! it happens all the time with me too by working too much on the same thing. Thank god! we have SO :)
0

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

But your with needs to be outside the else block.

answered Nov 19, 2019 at 16:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.