Re: Insert with a lot of columns

From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: David Kerr <dmk(at)mr-paradox(dot)net>, psycopg(at)postgresql(dot)org
Subject: Re: Insert with a lot of columns
Date: 2013-12-31 18:50:21
Message-ID: 52C311ED.1070701@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: psycopg

On 12/31/2013 10:35 AM, David Kerr wrote:
> Howdy all
>
> I’m a bit of a python noob so bear with me if this is obvious but i’m
> trying to insert into a table with a lot of columns (486 columns)
>
> so far i’ve got
>
> query_string = “””
> INSERT INTO TABLE ( <stuff> )
> VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, ...

>
> Now, the good news is that i have a RealDictCursor with all the data in it that i want to insert (in the right order)

First to use a dictionary to supply parameters you need to use the named
format( %(name)s ) for the placeholders. So the above is not going to
work. See here for more detail:

http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries

This would seem to be a case where using INSERT INTO SELECT * FROM might
work better:

http://www.postgresql.org/docs/9.3/interactive/sql-insert.html

>
> so i’d love to be able to do
>
> cur.execute(query_string, row)
>
> but that generates an error:
> Traceback (most recent call last):
> File "./mover.py", line 1540, in <module>
> do_insert(conn, row2)
> File "./mover.py", line 498, in do_insert
> cur.execute(insert_sql, row)
> File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psycopg2/extras.py", line 223, in execute
> return super(RealDictCursor, self).execute(query, vars)
> KeyError: 0
>
> I actually went through and did
> cur.execute(query_string, row[‘first_column’],row[‘second_column’]…)
>
> and that generated an error about “More than 255 arguments”
>
> Any suggestions on how to make this work?
>
> Thanks
>
>
>
>
>
>
>
>

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message David Kerr 2013-12-31 18:56:06 Re: Insert with a lot of columns
Previous Message David Kerr 2013-12-31 18:35:52 Insert with a lot of columns