Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

python - Handling errors when inserting data to Postgres SQL

I have a python code to insert dataframe data created from csv file to Postgres. My data are not edited, so I'm checking for errors when I'm inserting one-by-one line to the database.

Often I get error that my value is too long for type VARCHAR(15) etc.. It's okay, but when I get the error my data are not inserted at all. I would like to make the code continue inserting data when error occurs and not stop totally...


def df2db(conn: psycopg2.extensions.connection, df: pd.DataFrame, table: str):
    columns = [col for col in df.columns]
    buf = StringIO()
    df.to_csv(buf, sep='', na_rep='\N', index=False, header=False)
    buf.seek(0)
    cursor = conn.cursor()
    for row in buf:
        row = row.replace("
", "").split("")
        row = ["'" + val + "'" for val in row]
        try:
            cursor.execute(f"INSERT INTO {table} ({','.join(columns)}) VALUES({','.join(row)}) ON CONFLICT DO NOTHING")
        except psycopg2.Error:
            conn.rollback()
            continue # here it continues, but my data are not inserted
    cursor.close()

BTW. I have about 20mil records, I can't do expensive processing


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No “expensive” processing needed. Just cut the strings to their field lengths according to the database schema - either on Python side, or using functions in the query.

But I’d do it differently: use pg tool or pgAdmin or the COPY sql statement to load the CSV as-is into a temporary table, since that will be very fast, and then do a query that copies the data over, cutting strings to maximum length.

See this q&a for more details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...