Re: problem with simple insert(newbie)

Daniele Varrazzo <[email protected]> Tue, 25 May 2010 22:14:09 +0100
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
On Tue, May 25, 2010 at 9:16 PM, uno dos <refreegrata-/[email protected]> wrote:
>
> Hello list.

Hello, welcome,

> In first place, i want a say that i'm a newbie with python and psycopg2. =
That is the=A0 reason of this simple question.
> I never used "psycopg2" before, and now i have problems with a simple sen=
tence.
>
> When i do this:
> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 asdas =3D 'asdas'
> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 self.__cursor.execute("insert into tabl=
a (informacion) VALUES (%s);",(asdas))
>
> Python throw the exception "<type 'exceptions.Exception'>not all argument=
s converted during string formatting"

The problem is that in Python, in order to have a tuple with a single
element, you need a comma (it's actually the comma that makes the
tuple, not the parentheses:

>>> print (1,2,3)
(1, 2, 3)
>>> print (1,2)
(1, 2)
>>> print (1)
1 # oops
>>> print (1,)
(1,) # you probably meant this

So you need something like self.__cursor.execute("insert into tabla
(informacion) VALUES (%s);",(asdas,))

The issue is mentioned in the FAQ
(http://initd.org/psycopg/docs/faq.html). In case of problems take a
look there, but you are very welcome to ask your question on the list
in case the docs don't solve your problems.

> that's all, thanks for read, and sorry for my poor english(i speak spanis=
h)

You are welcome. Cheers

-- Daniele