comparing results of fetchall() with a known value
jhardcastle at solarc.com
Tue Aug 21 01:28:19 CEST 2001
Hi - I'm new to Python, and am having trouble reading the results of a
database call using fetchall(). Here's my situation:
I have a database table containing a list of names, and I have a
string variable containing a name. I need logic that will determine
if my string exists in the database table. So I have a simple sql
like "select name from employees", and I have a string empname =
'Jones', and I need to verify that 'Jones' is in the employees list
(assume duplicate names are not an issue in this case).
Here's what I've done: I created an odbc connect to the database,
opened a cursor, executed the sql, then set a variable: namelist =
cursor.fetchall(). What's got me stumped is that "namelist" is a list
of tuples with sample data like: [('smith',), ('johnson',),
If this were a normal tuple or list, I could easily use:
"if empname in namelist:
But with the list of tuples, I don't know how to easily do this. My
hunch is that there must be some way to convert that list of tuples
into a list of single values, since I only need the first element of
the tuple like 'jackson' from ('jackson',). Once I had the list of
single values, my if statement above would work.
Thanks for your help!
More information about the Python-list