Re: problem with simple insert(newbie)
uno dos <refreegrata-/[email protected]> Tue, 25 May 2010 14:48:03 -0700 (PDT)
| Newsgroups | gmane.comp.python.db.psycopg.devel |
|---|---|
| Message-ID | <[email protected]> |
--===============3670451647074873183== Content-Type: multipart/alternative; boundary="0-1837197941-1274824083=:7865" --0-1837197941-1274824083=:7865 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable thanks, now are ok. I never imagined that was is the problem. you're the best P.D.: Ooppps. In the last mail, I forget send a copy to the mailing list --- On Tue, 5/25/10, Daniele Varrazzo <[email protected]> wrote: From: Daniele Varrazzo <[email protected]> Subject: Re: [Psycopg] problem with simple insert(newbie) To: "uno dos" <refreegrata-/[email protected]> Cc: psycopg-IAPFreCvJWPBWskQ1e/[email protected] Date: Tuesday, May 25, 2010, 4:14 PM 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 =0A=0A=0A --0-1837197941-1274824083=:7865 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable <table cellspacing=3D"0" cellpadding=3D"0" border=3D"0" ><tr><td valign=3D"= top" style=3D"font: inherit;">thanks, now are ok. <span id=3D"result_box" c= lass=3D"short_text"><span style=3D"" title=3D"">I never imagined that was i= s the problem.<br><br>you're the best</span></span><br><br>P.D.: Ooppps. In= the last mail, I forget send a copy to the mailing list<br>--- On <b>Tue, = 5/25/10, Daniele Varrazzo <i><[email protected]></i></b> wro= te:<br><blockquote style=3D"border-left: 2px solid rgb(16, 16, 255); margin= -left: 5px; padding-left: 5px;"><br>From: Daniele Varrazzo <daniele.varr= [email protected]><br>Subject: Re: [Psycopg] problem with simple insert(new= bie)<br>To: "uno dos" <refreegrata-/[email protected]><br>Cc: [email protected]= itd.org<br>Date: Tuesday, May 25, 2010, 4:14 PM<br><br><div class=3D"plainM= ail">On Tue, May 25, 2010 at 9:16 PM, uno dos <<a ymailto=3D"mailto:refr= eegrata-/[email protected]" href=3D"/mc/compose?to=3Drefreegrata-/[email protected]">refreegr= ata-/[email protected]</a>> wrote:<br>><br>> Hello list.<br><br>Hello, welcome,<br><br>> In f= irst place, i want a say that i'm a newbie with python and psycopg2. That i= s the reason of this simple question.<br>> I never used "psycopg2"= before, and now i have problems with a simple sentence.<br>><br>> Wh= en i do this:<br>>  = ; asdas =3D 'asdas'<br>>  = ; self.__cursor.execute("insert into tabla (i= nformacion) VALUES (%s);",(asdas))<br>><br>> Python throw the excepti= on "<type 'exceptions.Exception'>not all arguments converted during s= tring formatting"<br><br>The problem is that in Python, in order to have a = tuple with a single<br>element, you need a comma (it's actually the comma t= hat makes the<br>tuple, not the parentheses:<br><br>>>> print (1,2= ,3)<br>(1, 2, 3)<br>>>> print (1,2)<br>(1, 2)<br>>>> print (1)<br>1 # oops<br>>>> print (1,)<br>(1,) # you probably me= ant this<br><br>So you need something like self.__cursor.execute("insert in= to tabla<br>(informacion) VALUES (%s);",(asdas,))<br><br>The issue is menti= oned in the FAQ<br>(<a href=3D"http://initd.org/psycopg/docs/faq.html" targ= et=3D"_blank">http://initd.org/psycopg/docs/faq.html</a>). In case of probl= ems take a<br>look there, but you are very welcome to ask your question on = the list<br>in case the docs don't solve your problems.<br><br>> that's = all, thanks for read, and sorry for my poor english(i speak spanish)<br><br= >You are welcome. Cheers<br><br>-- Daniele<br></div></blockquote></td></tr>= </table><br>=0A=0A --0-1837197941-1274824083=:7865-- --===============3670451647074873183== 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 --===============3670451647074873183==--