Feb-17-2022, 08:27 PM
I am using pandas to read data from SQL with some specific chunksize and loading it into postgres.
I am reading a files that contain the script to insert data into table.
I am having problem with my loop because it's not going to the next line. it comes back and gets the same data and with that I get the error:
Could anyone explain me what is wrong here?
I am reading a files that contain the script to insert data into table.
I am having problem with my loop because it's not going to the next line. it comes back and gets the same data and with that I get the error:
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "vendor_pkey"this is my code bellow (not whole code)
Could anyone explain me what is wrong here?
try: connection = psycopg2.connect( database="my_database", user="user", password="my_password", host="my_host", port="port") try: with open('{0}{1}.sql'.format('path_sql', 'tables', 'r', errors='ignore') as sql: files = sql.read() except FileNotFoundError: raise except Exception as error: raise error ind = 0 for df in pd.read_sql(query, con, chunksize=10000): # create a cursor cur = connection.cursor() for i in df: # execute a statement i = eval(f'f"""{files}"""') cur.execute(files) ind = ind + 1 cur.close() except (Exception, psycopg2.DatabaseError) as error: connection.rollback() raise error finally: if connection is not None: connection.commit() connection.close() return "End loads"I do appreciate for any help