Strange MySQL Problem...

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Apr 3 14:22:29 EDT 2008


En Thu, 03 Apr 2008 14:03:53 -0300, Victor Subervi  
<victorsubervi at gmail.com> escribió:

> On Thu, Apr 3, 2008 at 9:34 AM, Gabriel Genellina  
> <gagsl-py2 at yahoo.com.ar>
> wrote:
>
> Thank you. I believe you mean by bound, something like this, right?
> binary(picdata)
> I am now doing that, if I am not mistaken.

No, I mean execute(sqltext, arguments) instead of  
execute(sqltext_with_values_inserted_by_hand)

>> *post*? This is the server response. Return either text *or* an image  
>> (the
>> text may be an html referencing the image)
>
> Yes, post. But what do I write instead of this?
> print 'Content-Type: image/jpeg\r\n'

Perhaps you should read a little about how HTTP works.
Very shortly, the client (the browser, on behalf of the user) sends an  
HTTP Request (usually a GET request, sometimes POST). The server receives  
it, processes it and returns an HTTP Response. Whatever you 'print' in  
your CGI script is part of the Response, a response isn't a POST.
If the response is an HTML page, the Content-Type should say text/html,  
NOT image/jpeg.

>> Test it locally (just the database thing, no web), test the cgi script
>> without database interaction, only then join the two.
>
> Okay, but how do I upload an image into mysql without a script? And once  
> I can do that, everything´s (almost) solved!

I insist: test each step separately. First locally, later merge all steps.  
One doesn't "upload an image into mysql": one uploads an image, and some  
script on the server saves the image into mysql database.

>> But you can read the server logs at least?
> I am new to Plesk. I discovered I can read logs. More following...

Good thing!

>> Your code is throwing exceptions but you're not seeing them. Use the  
>> cgitb
>> module http://docs.python.org/lib/module-cgitb.html
>
> Now here you give me another laugh :) I added this line to the script
> import cgitb; cgitb.enable()
> and suddenly I can take out these junk lines that were throwing the HTTP  
> 200
> error:

Please stop refering to it as HTTP 200 error: it is *not* an error!

> I have a script called start.py which has this code in it:
>
>   col_names = ['id', 'name_en', 'name_es', 'name_it', 'category',
> 'title_en', 'title_es', 'title_it', \
>     'description_en', 'description_es', 'description_it', 'price',
> 'bedrooms', 'bathrooms', 'pic1', 'sort_factor']
> Just notice the pic1 there...
> Then I make a nice list of colnames like MySQL wants...
>
>   col_names_with_commas = ''
>   for name in col_names:
>     col_names_with_commas += name + ', '
>   count = len(col_names_with_commas)-2
>   col_names_with_commas =
> col_names_with_commas[0:len(col_names_with_commas)-2]

The above code is a long and convoluted way to write:

     col_names_with_commas = ', '.join(col_names)

>     for id in ids:
>       for d in id:
>         print '<tr>\n'
>         cursor.execute('select ' + col_names_with_commas + ' from  
> products
> where id = ' + str(d) + ';')

Remember to use parameters:
   cursor.execute('select ' + col_names_with_commas +
                  ' from products '
                  ' where id = %s;', (d,))

All this looks like the kind of spaghetti code common in cgi scripts  
around 10 years ago, mixing application logic, storage and presentation  
all at once.
At least try to separate database operations from html generation: make  
your queries, build some data structure, and generate the HTML using that  
structure as parameters.
There are many templating engines to choose for:  
http://wiki.python.org/moin/Templating

>           if x == 15:
>             print '<td>', field, '</td>\n'
>           else:
>             print '<td>', field, '</td>\n'
>           x += 1
> I have to do both of those for statements. The sticky point is in the  
> last
> if statement. 15 is the pic1 field. Should I use binary(field) instead?  
> It
> does not like that.

Binary is part of the DBAPI interface (you should read PEP 249 [1]). It  
has absolutely nothing to do with html pages.

> mod_python.cgihandler: NameError: global name 'binary' is not defined,

Python is case sensitive... Use MySQLdb.Binary instead. See [1] and the  
MySQLdb documentation.
I insist again: this would have been a lot easier to catch if you test  
locally; this error has nothing to do with the web part of the application.

[1] http://www.python.org/dev/peps/pep-0249/

-- 
Gabriel Genellina




More information about the Python-list mailing list