Why is my Python Tuple only storing first value?
<p>I am storing a mysql query w/in a tuple in my python script. I have a raw_input variable w/in the script which I would like to check if their is a match w/in the tuple.</p>

<pre><code>curs.execute("""SELECT username, id FROM employees WHERE gid!=4""")
rows=curs.fetchall()
db.close()

uname = raw_input('Type the username for the new device: ')

for row in rows:
  user = row[0]
  empid = row[1]

  if user == uname:
    print 'User ' + uname + ' found...'
  else:
    print 'Not Found'
</code></pre>

<p>This only works if I type the username of the first employee.  Any other employee returns 'Not Found'</p>

<p>I noticed that if I remove the raw_input line, and the if statement and add </p>

<pre><code>for row in rows:
  user = row[0]
  empid = row[1]
  print user
</code></pre>

<p>It prints all of the values</p>

<p>This may be a better approach based on what I assume you are trying to do.</p>

<p>There is no reason for the loop in Python to search for the user when you can simply add that to your <code>WHERE</code> clause.</p>

<pre><code># Get the username from input
uname = raw_input('Type the username for the new device: ')

# Check to see if username in table
curs.execute("SELECT username, id FROM employees WHERE gid != 4 AND LOWER(username) = %s", (uname.lower(),))
row = curs.fetchone() # If no records found, should return an empty tuple
db.close()

# Return the user if found
if row:
    user = row[0]
    empid = row[1]
    print 'User ' + user + ' found...'
else
    print 'Not Found'
</code></pre>

<p>This will return the username, if found. Otherwise 'Not Found'.</p>

python mysql tuples