Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error handling
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,216 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Help needed with a "for loop" + error handling tamiri 2 2,387 May-27-2022, 12:21 PM
Last Post: tamiri
  Handling Python Fatal Error richajain1785 7 5,758 Oct-14-2021, 01:34 PM
Last Post: Tails86
  Error Handling JarredAwesome 5 2,842 Oct-17-2020, 12:41 AM
Last Post: JarredAwesome
  Error handling using cmd module leifeng 3 2,801 Jun-06-2020, 06:25 PM
Last Post: leifeng
  Excpetion Handling Getting Error Number gw1500se 4 2,326 May-29-2020, 03:07 PM
Last Post: gw1500se
  Warning / Error handling in python Prarthana_12 1 5,060 Feb-08-2019, 09:21 PM
Last Post: snippsat
  Help With Error Handling jo15765 6 4,044 Sep-14-2018, 06:27 PM
Last Post: jo15765
  Error Handling/No results from SQL Query JP_ROMANO 7 9,257 Jul-18-2018, 02:31 PM
Last Post: JP_ROMANO
  Error Handling is weird PythonAndArduino 1 2,961 Nov-09-2017, 05:08 AM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020