Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem passing Tuple to MySQLdb in python
    primarykey
    data
    text
    <p>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..</p> <p>for example:</p> <p><strong>ORIGINAL:</strong><br> (This works, so ListID etc is definitely valid)</p> <pre><code>sql="SELECT NAME FROM ListsTable WHERE ID=%s"%(ListID) c.execute(sql) </code></pre> <p><strong>ATTEMPTED WITH TUPLE:</strong> </p> <pre><code>sql="SELECT NAME FROM ListsTable WHERE ID=%s" c.execute(sql,(ListID,)) </code></pre> <p><strong>WORKS:</strong> </p> <pre><code>sql="SELECT NAME FROM ListsTable WHERE ID=%s" c.execute(sql, ListID) </code></pre> <p>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:</p> <p><strong>ORIGINAL:</strong> </p> <pre><code>sql="SELECT * FROM ListsTable ORDER BY ID DESC LIMIT %s,%s"%(Page, (Page*20)+20) c.execute(sql) </code></pre> <p>which works, but then this doesn't, again if i try to send the parameters as a tuple:</p> <pre><code>sql="SELECT * FROM ListsTable ORDER BY ID DESC LIMIT %s,%s" var1=Page var2=(Page*20)+20 params=(var1,var2) c.execute(sql,params) </code></pre> <p>or even just </p> <pre><code>sql="SELECT * FROM ListsTable ORDER BY ID DESC LIMIT %s,%s" c.execute(sql,(Page,(Page*20)+20)) </code></pre> <p>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)</p> <pre><code>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 </code></pre> <p>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..</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COIt would help enormously if you gave more information than "doesn't work" in the first few cases, and showed what you were executing when you got that logged error. Please note that you can and should experiment around your problems at the Python interactive prompt (`>>>`) and/or in very simple scripts that you can run at your shell command-line -- avoid irrelevancies like your web server which make debugging simple problems harder. Show your data e.g. `print repr(ListID)`.
      singulars
    2. COIt all works fine in the code above, I don't get why it's such an issue to move it from string substitution to tuple parameterisation when the docs say it only accepts tuples. I would've thought that'd be an easy error to spot for anyone who isn't me. It's part of a CGI script that reads and returns JSON structures to an Android Phone so installing a local apache webserver and adapting inputs just to debug something that is probably very obvious might be counterproductive, especially as the actual script but for this is finished. Also, when the first lot don't work there is no error.
      singulars
    3. COAs I said, all of that CGI/JSON/Android/apache stuff is quite irrelevant to your alleged problem with MySQLdb. I certainly wasn't inciting you to install a local apache webserver, but to do some experiments that do relate to your problem, as @jsw has done. """when the first lot don't work, there is no error""" so what does "don't work" mean exactly? If you can't reproduce your problem in a manner similar to that of @jsw, you'll need to log (1) your sql statement (2) the argument tuple (3) the fetchall() result (in each case using `repr()` to avoid ambiguity) and show us the results.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload