[Tutor] MySQL Connection Function

Don Parris webdev at matheteuo.org
Tue Jun 21 21:05:00 CEST 2005

As a newbie developer, the easiest way for me to connect to MySQL is to
just copy & paste the connection commands into each funtion I write. 
However, I know that's far from ideal, and consumes more time than its
worth.  I would like to create a MySQL connection function that I can just
call up whenever I need it from within an other function.

### The Connection Definition ###
# def mysql_Conn():
    # Create a connection object and create a cursor.
    # Con = MySQLdb.Connect(host="", port=3306, user="user",
      passwd="password", db="chaddb_test")   # email text wrap here
    # Cursor = Con.cursor()

### The function that Calls the Connection ###
def mbr_Roster():
    # Make SQL string and execute it.
    sql = "SELECT fst_name, lst_name FROM person\
        where env_num is not null\
        order by lst_name"

    # Fetch all results from the cursor into a sequence and close the
    # connection.    
    Results = Cursor.fetchall()

How do I get mbr_Roster() to recognize the 'Cursor' from mysql_Conn()?  Do I
need to declare the cursor as a global variable?

>From the Traceback:
global name 'Cursor' is not defined

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