1

I am trying to get psycopg2 to accept the list of tuples I'm inputting but it is failing because the tuples are inside a list with [] brackets and I have tried to convert the list to a tuple so it's a tuple of tuples to no avail. Here is a sample of the tuples variable:

[('AAA Service Network', 'null', 'http://www.aaaservicenetwork.com/', 'null', 'null', '.', '.', 'Moneyball', 'United states', 'yellow_pages_scrape', 'yellow_pages_scrape2022-01-12', 'HVAC', 'null', 'null', 0), ('Service Experts Heating & Air Conditioning', 'null', 'http://www.serviceexperts.com/', 'null', 'null', '.', '.', 'Moneyball', 'United states', 'yellow_pages_scrape', 'yellow_pages_scrape2022-01-12', 'HVAC', 'null', 'null', 0),...]

here is the function:

def execute_values(conn, cur, df, table):
    tuples = [tuple(x) for x in df.to_numpy()]
    temp_table = "temp_yellow_pages"
    cols = ', '.join(list(df.columns))
    
    # SQL query to execute
    temp_to_perm_query = f"""INSERT INTO {table} ({cols})
                                SELECT {cols}
                                FROM {temp_table}
                                ON CONFLICT (company, website, postal_code) DO NOTHING;"""
    try:
        cur.execute('''CREATE TEMPORARY TABLE temp_yellow_pages (ID SERIAL Primary Key,
        company Text NUll,
        phone Text NUll,
        website Text NUll,
        address Text NUll,
        street Text NUll,
        city Text NUll,
        postal_code REAL NUll,
        state Text NUll,
        first_name Text NUll,
        last_name Text NUll,
        weekly_scrape Text NUll,
        strikezone Text NUll,
        lead_source Text NUll,
        country Text NUll,
        acquisition_source Text NUll
        );''')
        cur.execute(f"INSERT INTO {temp_table}({cols}) VALUES {tuples};")
        cur.execute(temp_to_perm_query)
        conn.commit()
        see_results(cur, table)
    except (Exception, psycopg2.DatabaseError) as error:
        print("Error: %s" % error)
        conn.rollback()
        # cur.close()
        return 1
    print("the dataframe is inserted")
    cur.close()

I'm not really sure what to do from here. Any help appreciated!

Justin Benfit
  • 423
  • 3
  • 11

0 Answers0