Mar-20-2020, 02:43 PM
Hello,
I typically write small programs for Python and never attempted to use logging or exception handling. This is all new to me, but with this program I'm currently writing I would like to incorporate error handling and file logging.
I'm receiving the following error when attempting to do so:
1 - It seems my exception is failing, but unsure why?
2 - Is there anywhere else in the code I should attempt to add logging and handling?
Thanks,
Frank
I typically write small programs for Python and never attempted to use logging or exception handling. This is all new to me, but with this program I'm currently writing I would like to incorporate error handling and file logging.
I'm receiving the following error when attempting to do so:
Error:Traceback (most recent call last):
File "C:/Users/AnelliaF/.PyCharmCE2019.3/config/scratches/sql_create_csv_w_logging.py", line 15, in main
conn = pyodbc.connect('Driver={SQL Server};'
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53)')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/AnelliaF/.PyCharmCE2019.3/config/scratches/sql_create_csv_w_logging.py", line 45, in <module>
main()
File "C:/Users/AnelliaF/.PyCharmCE2019.3/config/scratches/sql_create_csv_w_logging.py", line 20, in main
print("Error %d: %s" % (e.args[0], e.args[1]))
TypeError: %d format: a number is required, not strHere is the code:import pyodbc
import csv
import logging
import sys
def main():
#logging.basicConfig(filename='c:\\temp\\test.log', format='%(filename)s: %(message)s', level=logging.DEBUG)
# Configure logging
logging.basicConfig(filename='c:\\temp\\test.log', format='%(asctime)s: %(message)s', level=logging.DEBUG)
logger = logging.getLogger()
logger.info('Connecting to database...')
# Create connection object that represents the database
try:
conn = pyodbc.connect('Driver={SQL Server};'
'Server=MJ;'
'Database=AdventureWorks2012;'
'Trusted_Connection=yes;')
except pyodbc.Error as e:
print("Error %d: %s" % (e.args[0], e.args[1]))
sys.exit(1)
logger.info('Selecting records...')
cursor = conn.cursor()
script = """
SELECT TOP 10 * FROM person.person
"""
cursor.execute(script)
logger.info('Writing to CSV file...')
with open("c:\\temp\\csv_from_sql.csv", 'w') as csv_from_sql:
csv_writer = csv.writer(csv_from_sql, delimiter=',', lineterminator='\n')
# Write field name header line
csv_writer.writerow([i[0] for i in cursor.description])
# Write data rows
# Use enumerate() method to count rows
for row_num, row in enumerate(cursor, start = 1):
csv_writer.writerow(row)
logger.debug(f'Number of records: {row_num}')
#print(f'Number of rows: {row_num}')
logger.info('Finished writing file')
if __name__ == '__main__':
main()I have two questions:1 - It seems my exception is failing, but unsure why?
2 - Is there anywhere else in the code I should attempt to add logging and handling?
Thanks,
Frank
