Another Screwy Problem

J. Clifford Dyer jcd at sdf.lonestar.org
Fri Jan 8 16:44:55 EST 2010


Victor Subervi wrote:
> Hi;
> I have this line of code:
>  sql = 'select Name, Price from %sPackages where ID=%s;' % (store, pid)
> which prints to this:
>  select Name, Price from productsPackages where ID=1;
> which when I enter it into the MySQL interpreter gives me this:
> mysql> select Name, Price from productsPackages where ID=1;
> +------+--------+
> | Name | Price  |
> +------+--------+
> | pkg  | 123.45 |
> +------+--------+
> 1 row in set (0.00 sec)
> 
> exactly what I expect. However, in my script for some reason it returns
> this:
> ((1,),)


First, never use string formatting to pass parameters to your database.  Read the MySQLdb documentation (or sqlite, or psycopg2) documentation for reasons why, and how to do it right.

Second, in the same documentation, look up anything having the word "fetch" in it.  That should show you how to get the data you want

Third, please be more specific in your questions.  You say "In my script for some reason *it* returns this: ((1,),)," but you don't show us what "it" is.  How did you get that from your script?  So far we've got a variable called sql with your query in it.  How do I get the same wrong result you got?  I don't know.  I could pass it to a function that looks like this:

    def botch_sql_query(sql):
        return ((1,),)

I could furthermore "fix" it, so that it looks right, by doing this:

    def fixed_sql_query(sql):
        return(('pkg', 123.45),)

But that probably doesn't help.  Give us enough code to be clear, but not so much as to be overwhelming.  There's an essay called "How to ask smart question" by Eric Raymond that should help.

Cheers,
Cliff




More information about the Python-list mailing list