[Tutor] MySQLdb question

Patty patriciap.gu at gmail.com
Fri Apr 14 15:32:39 CEST 2006


Hi,

I have a data structure in a python file that looks something like this:

my_map= { "host1":  {"target1", "target2", "target3" },
           "host2":  {"target4", "target5", "target6" }, 
         }

I have a method that has two parameteres (ahost, atarget), which I want to use
to retrieve data from the database. In my database each host has a column and
each target has a row. The way I have it now is something like this:
 
cursor.execute("""SELECT host1, host2, host3, host4, host5, host6 FROM targets
        WHERE target_name = %s """, (target))

This is very inefficient because I'm retrieving data that I don't need. Instead,
I want to use the parameters in my method, but I'm not sure how to do it

cursor.execute("""SELECT %s FROM targets
        WHERE target_name = %s """, (ahost, target))  # I tried this, but it
didn't work.


I also tried this, but got a error:

cursor.execute("""SELECT %s FROM targets
        WHERE target_name = %s """ %  (ahost, target))

Can anybody show me the right way to do it?

Thanks,
Patty



More information about the Tutor mailing list