[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="127.0.0.1", port=3306,
user="user",
>       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
mysql_Conn()?
> 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.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list