简体   繁体   中英

How to continue loop and log error when validation error is raised in exception?

I'm pretty new to python but I have created a small script to insert rows in postgres, and a table that stores stock prices. The table have a unique constraint on date and ticker, to ensure only one price per ticker per day is inserted. I want the script to skip inserts and continue on with the rest, but I cannot figure out how to make the loop continue when the exception block is triggered.

The Python script is as follows:

def createConnection(db="db", user="john", password='doe', host='host', port=5432):
    conn = psycopg2.connect(
        database=db, user=user, password=password, host=host, port=port)
    return conn


def insertNewPrices(df):
    conn = createConnection()
    cur = conn.cursor()
    for row in df.head().itertuples(index=False):
        try:
            print(row)
            cur.execute(
                "INSERT INTO daily_price (price_date, ticker, close_price) VALUES (%s, %s, 
%s)", row)
        except psycopg2.IntegrityError as e:
            print("something went wrong")
            print(e)
            continue
    conn.commit()
    conn.close()

error raised:

psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "daily_price_ticker_price_date_key"
DETAIL:  Key (ticker, price_date)=(EQUINOR, 1990-02-28) already exists.

You have the insert statement outside the try statement. Can you remove the insert from outside and you should be OK.

    for row in df.head().itertuples(index=False):
        #cur.execute(
        #    "INSERT INTO daily_price (price_date, ticker, close_price) VALUES (%s, %s, %s)", 
row)
        try:
            print(row)
            cur.execute(
                "INSERT INTO daily_price (price_date, ticker, close_price) VALUES (%s, %s, 
%s)", row)
        except psycopg2.IntegrityError, psycopg2.errors.UniqueViolation) as e:
            print("something went wrong")
            print(e)

Also you don't need continue at the end of the except statement as its the last line.

Instead of checking for specific errors, you can also catch all errors and warnings using the below:

except (psycopg2.Error, psycopg2.Warning) as e:

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