我可以在 MySQL 查询中指定 db 吗?
python 193
原文标题 :Can i specify db in MySQL query?
这个问题在这里已经有了答案:Mysql union from multiple database tables (5 answers) Closed 7 mins ago 。
我有一个tableA
在同一主机下的4个不同数据库中具有相同的结构。有没有更好的方法,如何在 Python 中对这 4 个表进行垂直连接?
import pandas as pd
import MySQLdb
dbs = ["db1", "db2", "db3", "db4"]
config = ConfigParser()
config.read("config.ini")
for db in dbs:
connection = MySQLdb.connect(
user=config["db"]["user"],
passwd=config["db"]["password"],
host=config["db"]["host"],
db=db,
)
cursor = connection.cursor()
query = """
SELECT *
FROM tableA
"""
cursor.execute(query)
df = pd.DataFrame(cursor.fetchall())
# todo and now join all df into one
换句话说,我可以在查询中指定db1, db2, db3, db4
吗?