简体   繁体   中英

What is the best way to ignore errors when importing data from a pandas data frame to SQL Server?

I'm writing a python script to daily import data from a legacy system's data dump. I'd like to import the data and just skip rows that throw errors (eg wrong data-type). What is the best way of achieving this?

My current code:

engine = sqlalchemy.create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
conn = engine.connect()
df = pd.read_csv(file_path)
df.to_sql(tbl_name,conn,if_exists="append",index=False)

The file is rather large, so I'd prefer not iterating through rows as I have seen in some examples.

Shouldn't the df.to_sql just ignore those by default? I thought that's how it worked. If not, just setup a try catch routine.

try:
  engine = sqlalchemy.create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
  conn = engine.connect()
  df = pd.read_csv(file_path)
  df.to_sql(tbl_name,conn,if_exists="append",index=False)
catch:
  print('en erro was detected; please check...')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM