Type casting question

Does this make sense?

I have the following code in an action script (mouse clicked) in a list box.

If I use it as is, I see the correct string in SQLTag TempTag2. However, if I use “index1” rather than “index2” in the “query = …” statement, I get an error.

index1 = event.source.parent.Selected_Index + 1 fpmi.tag.writeToTag('[client]TempTag1', index1) index2 = fpmi.tag.getTagValue('[client]TempTag1') query = " AND group_order = \"" + index2 + "\"" fpmi.tag.writeToTag('[client]TempTag2', query)

Selected_Index is an integer dynamic property containing the Selected Index property from a list box.

TempTag1 and TempTag2 are string tags.

It appears that the “indexX” value is getting cast to string when it goes through TempTag1, but not cast correctly if it goes directly to the “query =…” instruction.

Is this the correct conclusion, or is something else going on? Thanks.

Your conclusion was spot-on. You can’t use the “+” operator to concatenate a string and an integer in Python. You could have cast the int to a string to make the concatenation work, like this:

index1 = event.source.parent.Selected_Index + 1 query = " AND group_order = \"" + str(index2) + "\"" fpmi.tag.writeToTag('[client]TempTag2', query)

Hope this helps,

Just for your edification, the more Python-esque way to do this would be:

index1 = event.source.parent.Selected_Index + 1 query = " AND group_order = '%d'" % index1 fpmi.tag.writeToTag('[client]TempTag2', query)

Also notice that I switched the double-quotes inside your SQL query to single quotes. You don’t use double quotes to quote literal strings in SQL. MySQL might let you get away with it, but its a habit that will come back to haunt you if you ever switch databases.

I’m not far at all into Jython. I don’t recognize the % operator(?). Could you translate this statement for me.

query = " AND group_order = '%d'" % index1

Thanks.

I don’t know the official term for it, but it’s kind of like saying “Ok, and here are all the things to replace the place holders in the string with”.

For example, say you had a string with 3 integer place holders (%d). You could do something like this:

print "Did you know, %d + %d = %d" % (1,2,3)

And it would stick those numbers in there in order. Note: with more than one parameter, you have to put them in parenthesis.

Hope this helps,

Cool. Thanks.

Its called string formatting or interpolation. Its kind of like a built-into-Python sprintf, if you’re familiar with C or C++.

Heres the docs on it:
docs.python.org/lib/typesseq-strings.html

Very convenient for creating queries in scripts, much better than trying to splice things together using the concatenation operator.