choose from a list

mensanator at mensanator at
Wed Oct 31 06:32:50 CET 2007

On Oct 30, 7:39?pm, barronmo <barro... at> wrote:
> I didn't know "result" was a list!  

I don't use MySQL but that's how others work.
Each list item is a record, each record a tuple
of field values.

> Can all that info be stored in a list?  

If you don't fetch too many records at once.
This is a test of my word database using ODBC
and MS-ACCESS (the SQL is very simple since
all the actual work is done in MS-ACCESS, Python
is just retrieving the final results).

import dbi
import odbc
con = odbc.odbc("words")
cursor = con.cursor()
cursor.execute("SELECT * FROM signature_anagram_summary")
results = cursor.fetchall()

Here, results (the recipient of .fetchall) is a list of tuples.
The contents are:

[(9, 10, 'anoretics', '10101000100001100111000000'),
(9, 10, 'atroscine', '10101000100001100111000000'),
(9, 10, 'certosina', '10101000100001100111000000'),
(9, 10, 'creations', '10101000100001100111000000'),
(9, 10, 'narcotise', '10101000100001100111000000'),
(9, 10, 'ostracine', '10101000100001100111000000'),
(9, 10, 'reactions', '10101000100001100111000000'),
(9, 10, 'secration', '10101000100001100111000000'),
(9, 10, 'tinoceras', '10101000100001100111000000'),
(9, 10, 'tricosane', '10101000100001100111000000')]

> How do the columns work?  

I don't know, I don't get column names. It looked like
from your example that you can use names, I would have
to use indexes, such as results[3][2] to get 'creations'.
Maybe MySQL returns dictionaries instead of tuples.

> I was curious to see what the data
> looked like but I can't seem to print "result" from the prompt.  Do
> variables used inside functions live or die once the function
> executes?  

Yeah, they die. You would have to have the function return
the results list and indx, then you could use it's contents
as criteria for further queries.

So you might want to say

name_find_results,indx = name_find(namefrag)

> If they die, how do I get around this?  

Add 'return results,indx' to the function. Or better still,
just return the record the user selected
    return results[indx]
You wouldn't need indx anymore since there's only 1 record.

> I tried defining 'r
> = ""' in the module before the function and then using it instead of
> "result" but that didn't help.
> Mike

More information about the Python-list mailing list