连接到两个数据库 [英] Connect to two databases
本文介绍了连接到两个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用Python连接到两个数据库,然后再使用两个数据库中的表.我怎样才能做到这一点?以下代码正确吗?
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)
推荐答案
如果未在connect
调用中指定数据库,则可以一次针对多个数据库编写查询. 文档表示不需要db
.
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')
然后
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.
这篇关于连接到两个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文