473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1203
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: dataangel | last post by:
I wrote a function to compare whether two strings are "similar" because I'm using python to make a small text adventure engine and I want to it to be responsive to slight mispellings, like...
1
by: CB | last post by:
Using C# in .Net 2003, DataSet.ReadXml fails when a percentage (%) sign is in the filename followed by 2 hex characters. Seems that the % sign is likely encoding the following 2 hex characters. ...
4
by: Trying_Harder | last post by:
Firstly, are functions defined in strings.h (`strcasecmp' and `strncasecmp') ANSI? if yes , then why am I getting an implicit declaration warning only with -Wall (Warnings all) flag? and if...
6
by: Sorin Sandu | last post by:
I want to find if a string var is equal with a number of strings. Something like var1 IN (or ==) "string1", "string2", "string3", .... Which is the best method?
16
by: Claudio Grondi | last post by:
What started as a simple test if it is better to load uncompressed data directly from the harddisk or load compressed data and uncompress it (Windows XP SP 2, Pentium4 3.0 GHz system with 3 GByte...
14
by: Dennis Benzinger | last post by:
Hi! The following program in an UTF-8 encoded file: # -*- coding: UTF-8 -*- FIELDS = ("Fächer", ) FROZEN_FIELDS = frozenset(FIELDS) FIELDS_SET = set(FIELDS)
11
by: Peted | last post by:
I see many c# code samples that relate to strings or using strings in file paths, that have an @ sign. eg string path1 = @"mydir"; string path2 = @"\mydir"; what does the @ do ?
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
15
by: allthecoolkidshaveone | last post by:
I want to convert a string representation of a number ("1234") to an int, with overflow and underflow checking. Essentially, I'm looking for a strtol() that converts int instead of long. The...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.