choose from a list
mensanator at aol.com
mensanator at aol.com
Tue Oct 30 19:03:31 EDT 2007
On Oct 30, 5:08 pm, barronmo <barro... at gmail.com> wrote:
> Thanks to both of you for the help. I made several of the changes you
> suggested and am getting the results in the format I want, eg:
>
> 0 387 John Smith
> 1 453 Jane Smith
> 2 975 Joe Smithton
>
> My plan at this point is, in addition to printing the results of the
> query, to create a list with a matching index so the user can select a
> name and I can store the patient_ID to get other parts of their
> medical record. Does this sound reasonable?
Don't you already have this list, called 'result'?
Once the user selects the index (let's say in a variable indx),
can't you build a SQL query to select records from other
tables (presumably keyed by patient_ID)?
Something like (made up)
cursor.execute("""
SELECT patient_ID, lab, test, test_result
FROM labtests
WHERE patient_ID=?""",
(result[indx]['patient_ID']))
>
> My code now looks like this:
>
> import MySQLdb
>
> def name_find(namefrag):
>
> conn = MySQLdb.connect(host = "localhost",
> user = "root",
> passwd = "Barron85",
> db = "meds")
> cursor = conn.cursor(MySQLdb.cursors.DictCursor)
> cursor.execute("SELECT patient_ID, firstname, lastname FROM
> demographics WHERE lastname LIKE '%s%%'" % (namefrag))
>
> result = cursor.fetchall()
> for index, row in enumerate(result):
> print "%d %s %s %s" % (index, row["patient_ID"],
> row["firstname"], row["lastname"])
> #create list here
>
> cursor.close()
> conn.close()
>
> Thanks again, Mike
More information about the Python-list
mailing list