使用python一个接一个地连接mysql表

使用python一个接一个地连接mysql表,python,mysql,Python,Mysql,我对python和mysql都是新手。我想使用python一个接一个地连接21个mysql表 我知道mysql中有UNION ALL函数可以实现这一点。但是,如何在python中实现它 我的代码是 import MySQLdb db = MySQLdb.connect() cursor = db.cursor() cursor1 = db.cursor() cursor.execute("SELECT * from table 1") cursor1.execute("SE

我对python和mysql都是新手。我想使用python一个接一个地连接21个mysql表

我知道mysql中有UNION ALL函数可以实现这一点。但是,如何在python中实现它

我的代码是

import MySQLdb

  db = MySQLdb.connect()
  cursor = db.cursor()
  cursor1 = db.cursor()
  cursor.execute("SELECT * from table 1")
  cursor1.execute("SELECT * from table 2")

现在在哪里使用UNION ALL…我需要为它创建21个游标吗…

在联合多个查询结果之后,您希望python返回什么样的格式?列表字典或者字符串?只使用一个游标。为每个表语法编写union all,然后最后使用cursor.executesql.for ACTION是否可以编辑文章并添加一些有关更改内容和更改原因的详细信息?它可以帮助问题的作者更好地理解你所做的事情,并且肯定会增加你回答的价值。谢谢。这项工作很好,但是合并两个文件需要很长时间。我想这21个文件将永远无法加入。我的档案很大。有没有更快的方法把它们结合起来。
import MySQLdb

db = MySQLdb.connect()

cursor = db.cursor()

sql="SELECT * FROM table1
UNION ALL
SELECT * FROM table2
UNION ALL;"

'''repeat for 21 table'''

try:

    cursor.execute(sql)
    db.commit()
except:

    db.rollback()

#I think this will do the join