利用Python从一个数据库中查询结果插入到另一个数据库中
import pymysql import pandas as pd DB = {'host': '****', 'port': ####, 'user': 'root', 'password': '#####', 'dbname': '%%%%'} def data_out_msql(DB, msql): conn = pymysql.connect(host=DB['host'], port=DB['port'], user=DB['user'], passwd=DB['password'], db=DB['dbname'], charset='utf8', cursorclass=pymysql.cursors.DictCursor) # 创建游标 cursor = conn.cursor() # 执行sql语句 cursor.execute(msql) y = cursor.fetchall() for value in y: write = pymysql.connect(host=DB['host'], port=DB['port'], user=DB['user'], passwd=DB['password'], db=DB['dbname'], charset='utf8', cursorclass=pymysql.cursors.DictCursor) Wcursor = write.cursor() value=value["id"] try: Wcursor.execute("insert into b (id) values (%s)",(value)) write.commit() except: write.rollback() write.close() cursor.close() conn.close() return y if __name__ == '__main__': mysql=""" select id from a """ a=data_out_msql(DB,mysql) print(a)