Re: problem with simple insert(newbie)

"Tim Roberts" <[email protected]> Tue, 25 May 2010 14:15:49 -0700
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
--===============5160885266427686439==
Content-Type: text/plain

You wrote:
>
>In first place, i want a say that i'm a newbie with python and psycopg2.
>That is the� reason of this simple question.

You should not feel bad; this is something that surprises most Python developers, even experienced ones.

>I never used "psycopg2" before, and now i have problems with a simple
>sentence.
>
>When i do this:
>   asdas = 'asdas'
>   self.__cursor.execute("insert into tabla (informacion) VALUES (%s);",(asdas))
>
>Python throw the exception "<type 'exceptions.Exception'>not all
>arguments converted during string formatting"

The issue here is that the second parameter to execute is required to be a tuple, but (asdas) is not a tuple.  It's just a variable name in parentheses.  To make a 1-item tuple, you need to include a comma:  (asdas,)

It's not intuitive, but there's a syntax problem here for which there is no easy solution.  You generally want (1+1) to be an integer, not a one-item tuple.  Hence, special syntax for tuples.
--
Tim Roberts, [email protected]
Providenza & Boekelheide, Inc.

--===============5160885266427686439==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Psycopg mailing list
Psycopg-IAPFreCvJWPBWskQ1e/[email protected]
http://lists.initd.org/mailman/listinfo/psycopg

--===============5160885266427686439==--