[Tutor] MySQLdb question
Allen John Schmidt, Jr.
ajschmidt at fredericksburg.com
Fri Apr 14 16:10:20 CEST 2006
Patty wrote:
>I have a data structure in a python file that looks something like this:
>
>my_map= { "host1": {"target1", "target2", "target3" },
> "host2": {"target4", "target5", "target6" },
> }
>
>cursor.execute("""SELECT %s FROM targets
> WHERE target_name = %s """ % (ahost, target))
>
>Can anybody show me the right way to do it?
>
>
Hi Patty,
I don't know if this is the right way, but here is basically how I would
do it.
First of all, I will assume that you are looping over the items in the
dictionary to get the variables ahost and target.
for ahost,target in my_map:
cursor.execute("""SELECT %s FROM targets
WHERE target_name in (%s) """ % (ahost, ",".join(target)))
This would generate a list that MySQL will look through to find
target_name. It is the same as one would do in Python to find an item in
a list:
if target_name in ("target1", "target2", "target3")
Hope that helps!
Allen J. Schmidt, Jr.
More information about the Tutor
mailing list