title: MySQL and Python Select Statement Issues
body: <p>Thanks for taking the time to read this. It's going to be a long post to explain the problem.  I haven't been able to find an answer in all the usual sources.</p>
  
  <p>Problem:
  I am having an issue with using the select statement with python to recall data from a table in a mysql database.  </p>
  
  <p>System and versions:</p>
  
  <pre><code>Linux ubuntu 2.6.38-14-generic #58-Ubuntu SMP Tue Mar 27 20:04:55 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
  Python: 2.7.1+
  MySql: Server version: 5.1.62-0ubuntu0.11.04.1 (Ubuntu)
  </code></pre>
  
  <p>Here's the table:</p>
  
  <pre><code>mysql&gt; describe hashes;
  +-------+--------------+------+-----+---------+-------+
  | Field | Type         | Null | Key | Default | Extra |
  +-------+--------------+------+-----+---------+-------+
  | id    | varchar(20)  | NO   | PRI | NULL    |       |
  | hash  | varbinary(4) | NO   | MUL | NULL    |       |
  +-------+--------------+------+-----+---------+-------+
  </code></pre>
  
  <p>Here are responses that I want via a normal mysql query:</p>
  
  <pre><code>mysql&gt; SELECT id FROM hashes WHERE hash='f'; 
  +------+
  | id   |
  +------+
  | 0x67 |
  +------+
  
  mysql&gt; SELECT id FROM hashes WHERE hash='ff'; 
  +--------+
  | id     |
  +--------+
  | 0x6700 |
  +--------+
  </code></pre>
  
  <p>As before, these are the responses that are expected and how I designed the DB.</p>
  
  <p>My code:</p>
  
  <pre><code>import mysql.connector
  from database import login_info
  import sys
  db = mysql.connector.Connect(**login_info)
  cursor = db.cursor()
  data = 'ff'
  cursor.execute("""SELECT
              * FROM hashes
              WHERE hash=%s""",
              (data))
  
  rows = cursor.fetchall()
  print rows
  for row in rows:
          print row[0]
  </code></pre>
  
  <p>This returns the result I expect:</p>
  
  <pre><code>[(u'0x67', 'f')]
  0x67
  </code></pre>
  
  <p>If I change data to :
      data = 'ff'
  I receive the following error:</p>
  
  <pre><code>Traceback (most recent call last):
   File "test.py", line 11, in &lt;module&gt;
      (data))
    File "/usr/local/lib/python2.7/dist-packages/mysql_connector_python-0.3.2_devel-    py2.7.egg/mysql/connector/cursor.py", line 310, in execute
      "Wrong number of arguments during string formatting")
  mysql.connector.errors.ProgrammingError: Wrong number of arguments during string formatting
  </code></pre>
  
  <p>OK.  So, I add a string formatting character to my SQL statement as so:</p>
  
  <pre><code>cursor.execute("""SELECT
              * FROM hashes
              WHERE hash=%s%s""",
              (data))
  </code></pre>
  
  <p>And I get the following response:</p>
  
  <pre><code>[(u'0x665aa6', "f'f")]
  0x665aa6
  </code></pre>
  
  <p>and it should by 0x6700.</p>
  
  <p>I know that I should be passing the data with one %s character.  That is how I built my database table, using one %s per variable:</p>
  
  <pre><code>cursor.execute("""
  INSERT INTO hashes (id, hash) 
  VALUES (%s, %s)""", (k, hash))
  </code></pre>
  
  <p>Any ideas how to fix this?</p>
  
  <p>Thanks.</p>