I want to execute an INSERT query via psycopg2, for this question let's simplify it to just:
query = """ INSERT INTO %s("%s") VALUES(%s); """
This works just fine when I do:
params = [AsIs(table_name), AsIs(column_name), value]
cursor.execute(query, params)
Now, my Pandas dataframe has about 90+ columns, I want to know what the best way to extend the query above to be able to execute it for multiple columns.
I have tried joining every column and value together as a single string and passing that in. I have also tried creating a string with 90+ "\"%s\""
and I have also tried creating a format string ie. """INSERT INTO {0} ({1}...{n}) VALUES ({n+1...n+n})""".format(...)
. There are unrelated issues that prevent these from working, but is there an easier way to handle this multiple column case?