Can't Encode Pic

Victor Subervi victorsubervi at gmail.com
Fri Nov 27 05:59:39 EST 2009


On Thu, Nov 26, 2009 at 5:08 PM, Dennis Lee Bieber <wlfraed at ix.netcom.com>wrote:

> On Thu, 26 Nov 2009 13:32:12 -0500, Victor Subervi
> <victorsubervi at gmail.com> declaimed the following in
> gmane.comp.python.general:
>
>
> > A problem occurred in a Python script. Here is the sequence of function
> > calls leading up to the error, in the order they occurred.
> >  /var/www/html/angrynates.com/cart/addEdit.py
> >    87   print '<body>\n</html>'
> >    88   db.commit()
> >    89   cursor.close()
> >    90
> >    91 addEdit()
> > addEdit = <function addEdit>
> >  /var/www/html/angrynates.com/cart/addEdit.py in addEdit()
> >    71       for pic in pics:
> >    72         sql = 'update %s set %s=%s where ID=%s;' % (t,
> > colNamesPics[i], '%s', str(id))
> >    73         cursor.execute(sql, (MySQLdb.Binary(pics[id]),))
>
>         Please study the Python language documents... AND the DB-API PEP...
>

I have read them from cover to cover many times. I just wish everything
stuck :(

>
>        sql = "update %s set %s=%%s where ID = %%s" % (t, colNamesPics[i])
>        #sets the table/field names, escapes the %s on the DB-API
> placeholders [MySQLdb, internally, uses string interpolation, which is
> why the placeholder is the same %s, which confuses many]
>
>        cursor.execute(sql, (MysQLdb.Binary(pics[id]), id))
>        # you don't have to use str(id) since that is the MEANING of %s as a
> placeholder -- generate a normal string representation of whatever the
> supplied parameter is (and the nature of MySQLdb is such that it WILL be
> a string with any internal quotes escaped, and wrapped in SQL quotes
> before getting to the replacement point).
>
> The following complained that there weren't enough arguments:

      for pic in pics:
        sql = 'update %s set %s=%%s where ID=%s;' % (t, colNamesPics[i],
'%s', str(id))
        cursor.execute(sql, (MySQLdb.Binary(pics[int(i)]),), )

Thus, it appears that using a % to escape a % is not what is called for
here! Did I miss something again? So I changed it to:

      for pic in pics:
        sql = 'update %s set %s=%s where ID=%s;' % (t, colNamesPics[i],
'%s', str(id))
        cursor.execute(sql, (MySQLdb.Binary(pics[int(i)]),), )

(diff: %s=%s)

That states that everything worked fine, and it does work fine when I insert
one image. However, when I go to update (the same code is used for the pics
for both insert and update), and try to insert a second image, it doesn't
insert even though it throws no error! If I try to print out all the
variables, including the actual image that is being updated, everything
prints out just fine! Why would the above statements work for inserting one
image but not a second??


>        Oh, and finally -- since you generate the SQL statement from scratch
> on each pass of the loop, why even bother with a variable named sql?
>
>        cursor.execute("""update %s
>                                                 set %s=%%s
>                                                where ID=%%s"""
>                                                         % (t,
> colNamesPics[i]),
>                                        (MySQLdb.Binary(pics[id]), id)  )
>
> Puts everything in one place, formats it in a somewhat more legible way
> (to me, at least, where the "where" clause is on a line by itself, the
> various "set" parameters are on lines, etc.; the Python string
> interpolation parameters are on one line, and the MySQLdb parameters are
> on another line)
>

I tried combining the two statements and that didn't work either. Same
problems as above.

>
>
>        I'm not going to look for the other problem -- this thread has shown
> a tendency of:
>
>        It doesn't work, what's wrong?
>
>        <answer to first problem>
>
>        I tried that, now it does this, why?
>
>        <answer to second problem>
>
>        etc.
>
> There has been no evidence of one ever referencing documentation in the
> attempt to resolve the matter on one's own.


It is becoming clear to me that utilizing the docs is an art. At least I
(perhaps because I am less adept at the art of programming than many) must
study the docs cover to cover many times, over many years (as my skills
improve), to get to the point where I know what to look for where and when I
need it, to know how to formulate the right questions and thus seek the
right answers on my own. It has taken me many years just to get to the point
of realizing that. And you have helped me to realize that now in your post
(as you have helped me many times with other issues).

Nothing hinting at having
> opened an interactive Python console and typing statements into it as an
> experiment to see what may work, or what changes may break something.
>

Please explain how this makes a difference. If I have the error in hand from
trying to run the code through the Web--either printed to screen or gathered
from the errors log--what need of opening the python interpreter, where I
would have to type everything in by hand (since I'm right now working on
others' computers and don't have right-click ability)?
TIA,
V
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091127/fc036a57/attachment.html>


More information about the Python-list mailing list