4

I am writing a script in Python and I am using MySQLdb package.

 con1 = mdb.connect('127.0.0.1', 'root', '', 'teacher') 
 con2 = mdb.connect('127.0.0.1', 'root', '', 'student', true) 

I can execute a query using a single cursor in python. But I want to write query to use tables from both the database at once. How can I do that?

2 Answers 2

4

Was looking for an answer to the same question. Found that connecting without specifying the database will allow you to query multiple tables:

db = _mysql.connect('localhost', 'user', 'password')

Then you can query different tables from different databases:

select table1.field1,
       table2.field2
from database1.table1 inner join
     database2.table2 on database2.table2.join_field = database1.field1.join_field

And boom goes the dynamite

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using PyMySQL and you are trying to create a query using multiple databases:

connection = pymysql.connect(
    host = "your_host",
    user = "your_user",
    password = "your_password",
    database = None,
    cursorclass = pymysql.cursors.DictCursor
)

The difference will be to pass the database variable as None

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.