[Tutor] MySQLdb question
Liam Clarke
ml.cyresse at gmail.com
Sat Apr 15 02:27:49 CEST 2006
On 4/15/06, Patty <patriciap.gu at gmail.com> wrote:
> 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
>
>>
>> for ahost,target in my_map:
>> cursor.execute("""SELECT %s FROM targets
>> WHERE target_name in (%s) """ % (ahost, ",".join(target)))
>>
>Hi again,
>I'm confused :-(
>I'm using an old version of python (2.3.4). In my database, target_name is the
>name of a column, not the name of a target.
I'm confused also.
Can you show us in SQL what you're trying to do? i.e. What's your
desired SQL statement?
More information about the Tutor
mailing list