Mar-19-2020, 01:23 PM
Hello,
I'm trying to count the number of records returned by a SQL query. The program writes to a .csv file, but I would like to output to a logfile showing any errors and the number of records returned.
The number count in place fails to return.
Any help would be great!
Thanks,
Frank
I'm trying to count the number of records returned by a SQL query. The program writes to a .csv file, but I would like to output to a logfile showing any errors and the number of records returned.
The number count in place fails to return.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
cursor = conn.cursor() script = """ SELECT TOP 10 * FROM person.person """ cursor.execute(script) 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 # fields = ['Field 1', 'Field 2'] # csv_writer.writerow(fields) csv_writer.writerow([i[ 0 ] for i in cursor.description]) # Write data rows for row in cursor: csv_writer.writerow(row) # Count the number of rows in output rows = cursor.fetchall() row_counter = 0 for row in rows: row_counter = + 1 print ( 'Number of rows %d' % row_counter) |
Thanks,
Frank