[Tutor] MySQLdb question

Pujo Aji ajikoe at gmail.com
Fri Apr 14 16:29:23 CEST 2006


I think you can wrap the select sql command with a function which return a
string.
this function receive a particular hostname in a string format and return
the whole 'SELECT ... FROM.. WHERE' style including the hostname from the
function argument.

Cheers,
pujo

On 4/14/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
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060414/8b685577/attachment.htm 


More information about the Tutor mailing list