469,306 Members | 2,549 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Strings and % sign fails - Help Please

I also posted this in Django Users group, but figured it probably has
more relevance for python group.

It seems like a freak problem to me. I spent a long hour to track the
problem down and here it is:

The following statement fails because it has the '%' sign in it.
cursor.execute("select '%'")

The error is: IndexError: list index out of range

How do I address this problem?

Please note that the following work just fine:
cursor.execute("select 'x'")

and the following also fails with the same error:
cursor.execute("""select '%'""")
cursor.execute("select '\%'")

astr = "select '\%'"
cursor.execute(astr)

I greatly appreciate all helps,
Regards,
Sia

Mar 24 '06 #1
10 1127
si*********@gmail.com writes:
The following statement fails because it has the '%' sign in it.
cursor.execute("select '%'")

The error is: IndexError: list index out of range

How do I address this problem?


Use "%%".
--
Jorge Godoy <go***@ieee.org>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
Mar 24 '06 #2
heh.. It works except I am using psycopg.Binary(somebinarystructure),
and I am not really doing it by hand to just add the extra %, and
psycopg.Binary doesn't do it. I'd imagine it's a bug with psycopg
package. Any quick way to project a string from freak '%' problems?

Thanks,
Sia
Jorge Godoy wrote:
si*********@gmail.com writes:
The following statement fails because it has the '%' sign in it.
cursor.execute("select '%'")

The error is: IndexError: list index out of range

How do I address this problem?


Use "%%".
--
Jorge Godoy <go***@ieee.org>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.


Mar 24 '06 #3
si*********@gmail.com writes:
heh.. It works except I am using psycopg.Binary(somebinarystructure),
and I am not really doing it by hand to just add the extra %, and
psycopg.Binary doesn't do it. I'd imagine it's a bug with psycopg
package. Any quick way to project a string from freak '%' problems?


Try using r"string '%'"...

--
Jorge Godoy <go***@ieee.org>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
Mar 24 '06 #4
Jorge Godoy wrote:
si*********@gmail.com writes:
heh.. It works except I am using psycopg.Binary(somebinarystructure),
and I am not really doing it by hand to just add the extra %, and
psycopg.Binary doesn't do it. I'd imagine it's a bug with psycopg
package. Any quick way to project a string from freak '%' problems?


Try using r"string '%'"...


Raw strings don't have anything to do with format specifiers.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
If the sky should fall, hold up your hands.
-- (a Spanish proverb)
Mar 24 '06 #5
Erik Max Francis <ma*@alcyone.com> writes:
Raw strings don't have anything to do with format specifiers.


I know. I'm just trying to see if there might be some magic going on with his
driver...

--
Jorge Godoy <go***@ieee.org>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
Mar 24 '06 #6
Jorge Godoy wrote:
I know. I'm just trying to see if there might be some magic going on with his
driver...


Since raw strings have no effect on format specifiers, that won't tell
you anything.
r'%' == '%'

True

His problem is that cursor.execute does format expansion with %, so a
single % is not legal.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
We are victims of our circumstance.
-- Sade Adu
Mar 24 '06 #7
Erik Max Francis wrote:
His problem is that cursor.execute does format expansion with %, so a
single % is not legal.


Yes, I think psycopg uses paramstyle='pyformat', i.e. it expands
parameters in your sql in the usual Python way where % has a special
meaning. If you really mean the % sign only, use %%.

-- Christoph
Mar 24 '06 #8
si*********@gmail.com wrote:
heh.. It works except I am using psycopg.Binary(somebinarystructure),
and I am not really doing it by hand to just add the extra %, and
psycopg.Binary doesn't do it. I'd imagine it's a bug with psycopg
package.


or a bug in your use of Binary. what exactly are you applying Binary
to, and how do you pass the resulting adapter to execute ?

</F>

Mar 24 '06 #9
Problem Solved. The problem was with psycopg.Binary whose mere job is
to convert bytes into clear text for sql statement. I'll be filing a
bug.

Thanks everyone,
Sia

Mar 24 '06 #10
Siah wrote:
Problem Solved. The problem was with psycopg.Binary whose mere job is
to convert bytes into clear text for sql statement.


no, its job is to wrap strings that contain binary blobs, so that data binding
works properly. you're supposed to do

cursor.execute(statement, Binary(data))

and not

cursor.execute(Binary(statement))

or

cursor.execute("some statement %s" % Binary(statement))

or some other sillyness. it's an object wrapper, not an escape function for
arbitrary SQL. Python's not PHP.

for more details, see "Type Objects and Constructors" on this page

http://www.python.org/dev/peps/pep-0249/

</F>

Mar 24 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

50 posts views Thread by dataangel | last post: by
4 posts views Thread by Trying_Harder | last post: by
6 posts views Thread by Sorin Sandu | last post: by
14 posts views Thread by Dennis Benzinger | last post: by
95 posts views Thread by hstagni | last post: by
15 posts views Thread by allthecoolkidshaveone | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.