<div class="gmail_quote">On Sat, Jan 9, 2010 at 9:07 AM, J. Cliff Dyer <span dir="ltr"><<a href="mailto:jcd@sdf.lonestar.org">jcd@sdf.lonestar.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="h5">On Sat, 2010-01-09 at 07:59 -0500, Victor Subervi wrote:<br>
> On Fri, Jan 8, 2010 at 4:44 PM, J. Clifford Dyer<br>
> <<a href="mailto:jcd@sdf.lonestar.org">jcd@sdf.lonestar.org</a>> wrote:<br>
> Victor Subervi wrote:<br>
> > Hi;<br>
> > I have this line of code:<br>
> > sql = 'select Name, Price from %sPackages where ID=%s;' %<br>
> (store, pid)<br>
> > which prints to this:<br>
> > select Name, Price from productsPackages where ID=1;<br>
> > which when I enter it into the MySQL interpreter gives me<br>
> this:<br>
> > mysql> select Name, Price from productsPackages where ID=1;<br>
> > +------+--------+<br>
> > | Name | Price |<br>
> > +------+--------+<br>
> > | pkg | 123.45 |<br>
> > +------+--------+<br>
> > 1 row in set (0.00 sec)<br>
> ><br>
> > exactly what I expect. However, in my script for some reason<br>
> it returns<br>
> > this:<br>
> > ((1,),)<br>
><br>
><br>
><br>
> First, got your other email. I thought I had executed the statement.<br>
> Oops. Works fine now. Sorry.<br>
><br>
> First, never use string formatting to pass parameters to your<br>
> database. Read the MySQLdb documentation (or sqlite, or<br>
> psycopg2) documentation for reasons why, and how to do it<br>
> right.<br>
><br>
> The only thing I found, which collaborates with something someone else<br>
> taught me on this list about entering binary data, is that one must<br>
> pass the parameters in the execute statement. Is that what you mean?<br>
> If so, I find that for all purposes thus far other than binary data,<br>
> the way I've been doing it seems to work just fine. I would prefer to<br>
> keep doing it that way, because I find putting a print statement<br>
> between the sql= line and the execute statement gives me a good<br>
> opportunity to review the sql statement and catch errors. Is this not<br>
> good practice?<br>
><br>
><br>
> Thanks.<br>
> beno<br>
<br>
</div></div>This is a horrendous practice. You leave yourself vulnerable not only<br>
to attacks, but to simple absent-mindedness as well. Using parameters<br>
in your execute statement will handle all necessary quoting for you,<br>
which eliminates the possibility of a bad query sneaking in. For more<br>
information, as I mentioned, look up SQL injection. Also, read this:<br>
<a href="http://xkcd.com/327/" target="_blank">http://xkcd.com/327/</a><br>
<br></blockquote>Thanks :)<br>beno<br>
</div>