title: Python create dynamic select statement for mysql
body: <p>In my MySQL database I have 10 tables.  Based on the user input, I want to select the rows from one of the table.  The user input is an image.  I am getting the number of columns and rows of pixels the image has and storing it in a variable.  </p>
  
  <pre><code>rows,cols,channels = img.shape
  w = int(cols)
  h = int(rows)
  l = w-10
  m = w+10
  p = h-10
  q = h+10
  area = w*h
  </code></pre>
  
  <p>the mysql tables are created basis the area of the image, and hence, depending on the area, I want to select the values from a specific table.</p>
  
  <pre><code>if (area &lt; 110980):
          sql = "select * from adf1 where WIDTH between %s and %s and HEIGHT between %s and %s;",(l,m,p,q)
      elif (area &lt; 182520):
          sql = "select * from adf2 where WIDTH between %s and %s and HEIGHT between %s and %s;",(l,m,p,q)
  else:
          sql = "select * from adf10 where WIDTH between %s and %s and HEIGHT between %s and %s;",(l,m,p,q)
  cur = mysql.connect().cursor()
  cur.execute(sql)
  </code></pre>
  
  <p>I am getting the following error:</p>
  
  <pre><code>TypeError: cannot concatenate 'str' and 'tuple' objects
  </code></pre>
  
  <p>However, when I pass on a single query within <code>cur.execute</code> I am able to retrieve the results.</p>
  
  <pre><code>cur.execute("select * from adf10 where WIDTH between %s and %s and HEIGHT between %s and %s;",(l,m,p,q))
  </code></pre>