Re: "ProgrammingError: can't adapt" when passing arguments to cursor.execute
William Carithers <wccarithers-/[email protected]>
| Newsgroups | gmane.comp.python.db.psycopg.devel |
|---|---|
| Message-ID | <C72BFBE4.10A70%[email protected]> |
Hi Tim,
Thanks for the suggestions. I don't think that Python versions are the
problem because when I first ran the script, it couldn't import psycopg2.
Then I remembered that I had upgraded to Mac OS 10.6 (aka Snow Leopard)
which ships with Python 2.6 since I last worked with psycopg2. Sure enough,
my old psycopg was in site-packages for Python 2.5. So I installed the
latest psycopg2 on Python2.6 and the script was happy to import. For the
interactive session, the Python version comes up automatically and it was
2.6.
Unfortunately changing load from a list to a tuple didn't solve the problem:
Traceback (most recent call last):
File "./loadDB.py", line 117, in <module>
sys.exit(main())
File "./loadDB.py", line 87, in main
curs.execute(query, tuple(load))
psycopg2.ProgrammingError: can't adapt
Same thing with changing load = (...) instead of load[...]
Thanks again,
Bill
On 11/19/09 4:10 PM, "Tim Roberts" <[email protected]> wrote:
> William Carithers wrote:
>> I'm trying to pass a list of arguments to an cursor.execute statement in a
>> Python script and I get the "can't adapt" error. However, when I execute the
>> same code snippet interactively, it works fine. Obviously, I'm missing
>> something here but I can't find it.
>>
>
> My GUESS is that you're somehow picking up a different versions of
> Python when you run it. Perhaps your #! line has an absolute path
> different from what you get at the command line.
>
> The versions of psycopg I'm familiar with don't accept lists as the
> argument, only tuples. So:
>
>> query = """INSERT INTO flags
>> VALUES
>> (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"""
>>
>> curs.execute(query, load)
>>
>
> Change that to:
> curs.execute( query, tuple(load) )
> and I'll bet it works.
>
> Or, change
>> load = [int(plate), int(mjd), fiber, z, id_type, flg[0],
>> flg[1],flg[2],flg[3], flg[4], flg[5],
>> flg[6], flg[7], flg[8], spflux[0], spflux[1], spflux[2], spflux[3],
>> spflux[4],'unscanned']
>
> to
>
> load = (int(plate), int(mjd), fiber, z, id_type, flg[0], flg[1],flg[2],flg[3],
> flg[4], flg[5],
> flg[6], flg[7], flg[8], spflux[0], spflux[1], spflux[2], spflux[3],
> spflux[4],'unscanned')