String Literal to Blob

Steve Holden steve at
Wed Apr 9 18:51:45 CEST 2008

Victor Subervi wrote:
> On Wed, Apr 9, 2008 at 11:10 AM, Steve Holden <steve at 
> <mailto:steve at>> wrote:
>     Victor Subervi wrote:
>         On Wed, Apr 9, 2008 at 10:24 AM, Steve Holden
>         <steve at <mailto:steve at>
>         <mailto:steve at <mailto:steve at>>> wrote:
>         connection = MySQLdb.connect(host=host, user=user,
>         passwd=passwd, db=db)
>         cursor = connection.cursor()
>         cursor.execute('select img from photo where id="7";')
>         content = cursor.fetchall()
>     Bzzt! Nope. Cursor.fetchall() returns a list (length 1) or tuples
>     (length 1) here, so this should be
>     content = cursor.fetchall()[0][0]
>     or, perhaps better
>     content = cursor.fetchone()[0]
> I tried both of these, and they, too, gave me the same identical image 
> of the url, not the image I am after.
I'm having a problem believing this, but I don't think you are lying. 
Are you *sure* you have stored the correct omages in your database?

The fact remains that cursor.fetchall() will return a list containing 
one tuple containing (what you believe is) your image, so there is NO 
way your code above can do what you want.

I can therefore only assume that this is a CGI script and that your web 
server does something *extremely* funky when it gets a CGI output it 
isn't expecting. But this doesn't make a lot of sense.

>     You really don't understand how the web works, do you?
> I am just really, really, stupid, Steve. Please forgive me for being so 
> stupid. But I am persistent. And I have built dozens of Web site with 
> images.
Stupidity and ignorance are entirely different things, and you (current) 
ignorance in no way implies stupidity. We all have to learn.

However, if you bring up one of the pages from one of your many web 
sites containing an image, and get your browser to display the HTML of 
that page you will surely find that the image does not appear direectly 
in the HTML, but instead appears as a tag in the HTML. Something like:

     <img src="http://server/path/image.jpg">

though the src attribute doesn't really need to be that complex.
>     In order to include an image in a page your browser must make TWO
>     requests. The first is for an HTML page that will reference the
>     image in this way:
>        <img src="http://your URL here">
>     Seeing this img tag causes the browser to make a SECOND request,
>     which the script I corrected above should respond to with an image.
>     The bytestream is critical in the image response. Even one misplaced
>     byte will mess things up terribly.
> In my stupidity, I have assumed you meant this:
>             content = col_fields[0][14].tostring()
>             print '<img src="', content, '"><br /><br />'
Well, here I have no idea what the content of your database might be, 
but if the fifteenth column you retrieve is the web server path to the 
graphic, that should be right except for the spaces around it, which 
might give trouble. You might consider instead

             content = col_fields[0][14].tostring()
             print '<img src="%s"><br /><br />' % content

to omit them.

> Obviously I am wrong. Could you please give me a little more insight?
Forget HTML for now. If you direct your browser to the URL on which your 
server is serving the graphic then it should be displayed in the browser 
window. Until that happy condition pertains, we are stabbing around in 
the dark.

>         BTW, when we are finally done with this, I will write a nice
>         how-to (since there is not one in python, while php has some
>         nice ones) on how to do this, and give you and Gabrielle all
>         your due credit. I will post it to this list, because that is
>         sure to rank highly in google right away.
>         Victor
>     That's great, though hardly the point of the exercise. I think
>     Google already know about Gabriel (*not* Gabrielle) and me already ...
> Well, I just thought it might be a good way to get the information out, 
> since it is not out there yet. And I believe it is only proper to give 
> credit where credit is due. I would not be doing this to praise you. 
> Rather, I would be doing this because it is needed, and I would feel 
> wrong for not crediting those who deserve credit. Please let me know, 
> however, if you feel otherwise.

It's not that I mind, but I do feel that this knowledge is already 
available, though clearly I might be wrong ...

Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC    

More information about the Python-list mailing list