Python Forum

Full Version: Error handling
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Error handling

Hi,
How to handle errors and exception in Python.
I have mixed columns in mytable with Integer and varchar in my sql database.
I am having a csv file with million of records in it
it consists of 20 columns.
Column3 is a string or varchar() in DB.
in DB Column3 has varchar(100).
suppose in the csv file in the column3 if the lenght is more than 100 char it is not inserted, rest of the rows also failing, no records has been inserted.
this kind of issues can be fixed with other tools, but how can we fix in Python.
how to seperate the lenght issue column while inserting into DB.
If below is my code

import pyodbc
cnxn = pyodbc.connect(r'DSN=myDSN;UID=userid;PWD=password')
crsr = cnxn.cursor()
crsr.execute("""BULK INSERT mytable
FROM 'D:\my.csv' WITH
(FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)""")
cnxn.commit() 
crsr.close()
cnxn.close()
How to handle errors and exception in Python in those cases.
Also how to commit for every 10K

Thanks in advance.