I have some sql queries that work through python mysqldb to my mysql database, but i want to make them a little less sql-injection vulnerable so little bobby tables doesn't try to add data..
for example:
ORIGINAL:
(This works, so ListID etc is definitely valid)
sql="SELECT NAME FROM ListsTable WHERE ID=%s"%(ListID)
c.execute(sql)
ATTEMPTED WITH TUPLE:
sql="SELECT NAME FROM ListsTable WHERE ID=%s"
c.execute(sql,(ListID,))
WORKS:
sql="SELECT NAME FROM ListsTable WHERE ID=%s"
c.execute(sql, ListID)
I don't know why that 2nd attempt doesn't work as a tuple but accepts it as a single parameter, but either way for another statement i need to pass multiple parameters so this won't work for that:
ORIGINAL:
sql="SELECT * FROM ListsTable ORDER BY ID DESC LIMIT %s,%s"%(Page, (Page*20)+20)
c.execute(sql)
which works, but then this doesn't, again if i try to send the parameters as a tuple:
sql="SELECT * FROM ListsTable ORDER BY ID DESC LIMIT %s,%s"
var1=Page
var2=(Page*20)+20
params=(var1,var2)
c.execute(sql,params)
or even just
sql="SELECT * FROM ListsTable ORDER BY ID DESC LIMIT %s,%s"
c.execute(sql,(Page,(Page*20)+20))
I've recently got this error on my webserver log, though note this MAY be a redherring due to the many different things I've been trying as it hasn't errored before: (The error refers to the above attempt of passing the "params" variable)
File "process.py", line 98, in main
c.execute(sql,params)
File "/var/www/cgi-bin/MySQLdb/cursors.py", line 159, in execute
query = query % db.literal(args)
TypeError: not enough arguments for format string
EDIT: in case it helps, i'm using mysqldb version 1.2.3, in case it didn't accept tuples at that version, but don't get me started on how rubbish the mysqldb docs are..