MySQL python: Cursor returns the variable name
<p>I saw before if someone ask the same but i didn't see a similar question. </p>

<pre><code>def get_session(Var = '', Key=''):
    conn = connection()
    cursor = conn.cursor()
    print "Var: "+Var
    cursor.execute("Select %(var)s FROM users WHERE sessions = %(key)s",{'var':Var,'key':Key})
    if(cursor.rowcount == 1):
        name = cursor.fetchone()
        print name
        return name
</code></pre>

<p>The output is like:</p>

<pre><code>get_session(Var = 'NAME', Key='asdf123')
</code></pre>

<blockquote>
  <p>Var:NAME</p>
  
  <p>('NAME',)</p>
</blockquote>

<pre><code>+----+----------+----------+---------------------------+
| ID | NAME     | PASS     |  sessions                 |
+----+----------+----------+---------------------------+
|  1 | Potato   | test     |  asdf123                  |
|  2 | asdf123  | test2    |  asdasd                   |
+----+----------+----------+---------------------------+
</code></pre>

<p>The correct output must be:</p>

<pre><code>get_session(Var = 'NAME', Key='asdf123')
</code></pre>

<blockquote>
  <p>Var:NAME</p>
  
  <p>('Potato',)</p>
</blockquote>

<p>or</p>

<pre><code>get_session(Var = 'PASS', Key='asdf123')
</code></pre>

<blockquote>
  <p>Var:PASS</p>
  
  <p>('test',)</p>
</blockquote>

<p>I supposed that the main problem is that mysql detects the name with quotes but i don't know how to fix it. </p>

<p>The problem was the quotes that sql put in automatically</p>

<pre><code>def get_session(Var = '', Key=''):
    conn = connection()
    cursor = conn.cursor()
    sql = "Select "+ Var+" FROM users WHERE sessions = %(key)s"
    print "Var: "+Var
    cursor.execute(sql,{'key':Key})
    print str(cursor.rowcount)
    if(cursor.rowcount == 1):
        name = cursor.fetchone()
        print name
        return name
    else:
        return None
    cursor.close()
    conn.close()
</code></pre>

<p>Thanks to all for responding.</p>

python mysql string cursor