<div class="gmail_quote">On Thu, May 27, 2010 at 2:54 PM, MRAB <span dir="ltr"><<a href="mailto:python@mrabarnett.plus.com" target="_blank">python@mrabarnett.plus.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

<div><div></div><div>Victor Subervi wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Hi;<br>
But what about this?<br>
<br>
  sql = "select pic%d from %s where ID='%%s';" % (pic, store)<br>
  cursor.execute(sql % id)<br>
<br>
If I try and rewrite the last line like this:<br>
<br>
  cursor.execute(sql, id)<br>
<br>
it doesn't work. What do?<br>
<br>
How about this one:<br>
<br>
        cursor.execute("insert into categories (Store, Category, Parent) values('%s', '%s', Null)", (store, cat))<br>
<br>
For some reason it puts single quotes around my variables! This doesn't happen if I change that comma for a percent sign! What do?<br>
<br>
How about this one:<br>
<br>
      sql = 'select * from options%s where ID=%%s', (opTable[0].upper() + opTable[1:])<br>
#      cursor.execute(sql, id)<br>
      cursor.execute('select * from options%s where ID=%s' % (opTable[0].upper() + opTable[1:], id))<br>
<br>
The last one works, but if I comment it out and uncomment the middle line, it doesn't. Same here:<br>
<br>
        sql = "update options%s set PriceDiff='%%s' where Field='%%s' and ID=%%s and Store='%%s'" % (opTable[0].upper() + opTable[1:])<br>
#        cursor.execute(sql, (value, opName, id, store))<br>
        cursor.execute('update options%s set PriceDiff="%s" where Field="%s" and ID=%s and Store="%s"' % (opTable[0].upper() + opTable[1:], value, opName, id, store))<br>
<br>
</blockquote></div></div>
As has already been explained, when working with SQL in Python there are<br>
2 forms of placeholder:<br>
<br>
1. Python's %s placeholder, replaced by Python's % operator.<br>
<br>
2. SQL's %s placeholder, replaced by the .execute method.<br>
<br>
SQL might not let you use its %s placeholder for table or column names,<br>
but they are normally hidden from the user and fixed by the application.<br>
<br>
For user-supplied values there's the risk of SQL-injection attacks.<br>
There are 2 ways of approaching that:<br>
<br>
1. The hard way: check the values and add any necessary quoting or<br>
escaping before using Python's % operator, then pass the fully-formed<br>
SQL statement to result to .execute.<br>
<br>
2. The easy way: pass the SQL statement to .execute with a %s for each<br>
value and let the method substitute the values itself (it'll add<br>
whatever quoting or escaping is necessary).<div><div></div><div><br>
<br></div></div></blockquote><div>Ok, so you're telling me I'm trying to do it the hard way. That's because I still don't have my head wrapped around the easy way. I was able to follow what Kushal Kumaran supplied; however I must still be lost on how that applies to the above examples. Could you illustrate with the first and let me try and figure out the rest?<br>
TIA,<br>beno  <br></div></div><br>