I want to connect to two databases using Python and, later on, use tables from both of the databases. How can I do this? Is the following code correct?
con = mdb.connect(host=MY_HOST, user=MY_USER, passwd=MY_PASS, db1=MY_DB1, db2=MY_DB2)
If you don't specify the database in your connect
call, you can write queries against multiple databases at once. The documentation says that db
is not required.
db = _mysql.connect('localhost', 'user', 'passwd')
then
SELECT u.*, i.* FROM db1.users u LEFT JOIN db2.items i ON u.id = i.user_id
But it'll only work if the two databases are on the same server.
Just make two separate connections
con1 = mdb.connect (host=MY_HOST, user=MY_USER, passwd=MY_PASS, db1=MY_DB1)
con2 = mdb.connect (host=MY_HOST2, user=MY_USER2, passwd=MY_PASS2, db2=MY_DB2)
and use them independently just as you would when using one database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With