You can keep things simple by splitting the queries:
cursor.execute('USE my_db')
cursor.execute('SELECT * FROM my_table')
GUI tools usually manage the context (i.e., the selected database) behind the scenes. Python connector is more strict—you need to explicitly fetch from each result in a multi=True call.
Edited to add context:
In the MySQL CLI or GUI tools (like DBeaver or MySQL Workbench), semicolon-separated statements are parsed and executed together.
But in mysql-connector-python, the behavior is different.
When you use:
cursor.execute('USE my_db; SELECT * FROM my_table;', multi=True)
It returns a generator of result objects, one per statement.
If you don’t iterate over that generator, none of the statements beyond the first are executed.
The USE my_db statement is executed. You never advance to the SELECT statement, so it’s not actually run. This explains why no rows are returned — the SELECT never ran.
You must iterate over the results:
rresults = cursor.execute('USE my_db; SELECT * FROM my_table;', multi=True)
for result in results:
if result.with_rows:
rows = result.fetchall()
print(rows) # or handle rows
multiin Connector/Python 9.2. Check Executing Multiple Statements. According to the docscursor.execute(sql_script)should work. To get results from all statements you should usemap_results=True. What version are you using?