Multiple database connections

In MySQL for Python, all database objects are discrete. All you need do is to connect with each under a different name. Consider the following:

mydb1 = MySQLdb.connect(host="localhost", 
                        user="skipper", 
                        passwd="mysecret", 
                        db="fish")
mydb2 = MySQLdb.connect(host="localhost", 
                        user="skipper", 
                        passwd="mysecret", 
                        db="fruit")
cursor1 = mydb1.cursor()
cursor2 = mydb2.cursor()

The objects then function like any other variable or object. By calling their methods and attributes separately, you can interact with either or even copy from one to the other.

Get MySQL for Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.