[Tutor] MySQL Connection Function

Alan G alan.gauld at freenet.co.uk
Wed Jun 22 00:52:48 CEST 2005

> 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.

Good idea!

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

        return Con.cursor()

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

No you need to return it from your function.
See the topic on modules and functions for more about
returning values from functions.

You might find you need to clear the cursor before using it too.


Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list