MySQL for Python
上QQ阅读APP看书,第一时间看更新

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.