Hello All,

I have a very simple search cursor that is selecting from a table a list of names in order to query a database. It was working fine until I encountered a name with an apostraphe in it. O'Brien for instance. The search cursor is very basic.

inCur = gp.SearchCursor(myFeatureClass2,"COLLECTED_BY = '" + str(Name) + "'", "", "", "DATE_COLLECTED A")

The term Name is retrieved from a list of values. When run it gets to O'Brien in the list and says that a value cannot be found. I know this is happening because of the single quotes that are around my input variable however I can't seem to find another way to word my search cursor to still find the names.

Any suggestions.

- Lee

Recommended Answers

All 4 Replies

what module is this gp?

module is from arcgisscripting

Try it the other way around?

inCur = gp.SearchCursor(myFeatureClass2,'COLLECTED_BY = "%s"' % (str(Name)), \
                                         "", "", "DATE_COLLECTED A")

Generally, the name is also stored as "obrien" so you can find O'Brien, O'brien, Obrien, etc. I don't know if this is possible here or not.

Thanks for the advice guys. In the end the solution was foudn as follows.

if "O'Brien" in Name:
        Name = Name.replace("'", "''")
    print 1
    inCur = gp.SearchCursor(myFeatureClass2,"COLLECTED_BY = '" + str(Name) + "'", "", "", "COLLECTED_BY A")

Adding the second ' allowed for the cursor to search normally.

Thanks again for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.