[Tutor] MySQL Connection Function (Solution)

Don Parris webdev at matheteuo.org
Fri Jun 24 07:43:53 CEST 2005

On Wed, 22 Jun 2005 15:06:22 -0400
Python <python at venix.com> wrote:

> On Wed, 2005-06-22 at 14:20 -0400, Don Parris wrote:
> > On Tue, 21 Jun 2005 19:13:43 -0400
> > Python <python at venix.com> wrote:
> > 
> > 

Here's my solution, using the code Lloyd provided in a previous post:
The script that contains the MySQL functions that the end-user will use most
imports the file containing the connection code:

def connect( parm_name):
    parms = db_parms[parm_name]
    return SQLdb.connect( **parms)

The file containing the core db functions uses do_Query to handle the cursor
and results, and return the results back to the functions, which then
continue their job of outputting the results.

def do_Query(sqlCmd):
    curs = conn.cursor()
    results = curs.fetchall()
    return results


def mbr_Roster(): 
    # Make SQL string and execute it.
    sqlCmd = "SELECT env_num, lst_name, fst_name FROM person\
        where env_num is not null\
        order by lst_name"
    Results = do_Query(sqlCmd)

The above line calling do_Query can be pasted into each function, and
provides exactly what I need (so far).

     # iterate through resultset.
    print 'The Church Membership Roster'
    for record in Results:
        print '%s ..... %s, %s' % record
        # print record
        mbrRoster = open('mbrRoster.txt', 'w')
        cPickle.dump(Results, mbrRoster)
    raw_input('Press ENTER to return to the menu.')

Due to the way the menu system is designed, raw_input allows me to hold the
output on the console screen until the user has verified the data that gets
written to the file.  Otherwise, the function returns control of the program
back over to the menu system, and the user is left looking at the menu after
seeing the output from the query flash on the screen for a second.

This may still be somewhat klunky, but it does what I need.  At the moment,
I'll leave well enough alone, but I would appreciate any feedback. 
Meanwhile, I appreciate the help and solutions offered.  Meanwhile, I do
need to go over some good examples of returning values, and passing
variables around.

evangelinux    GNU Evangelist
http://matheteuo.org/                   http://chaddb.sourceforge.net/
"Free software is like God's love - you can share it with anyone anytime

More information about the Tutor mailing list