我对 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 个游标吗...
最佳答案
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
关于python - 使用python将mysql表一个接一个地连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25562345/