1.python复制表
复制表语句:
create table newtable like oldtable;
insert into newtable select * from oldtable;
注:newtable:要复制的新表
oldtable:数据库中存在的表
python代码:
import pymysql conn = pymysql.connect(host='ares-m.dbsit.sfcloud.local', port=3306, database='ares', user='ares', password='7i6opxMmkj', charset='utf8') cursor = conn.cursor() cursor.execute('create table tt_flight_execute_flow12 like tt_flight_execute_flow;') cursor.execute('insert into tt_flight_execute_flow12 select * from tt_flight_execute_flow;') conn.commit() conn.close()
2.将xls中数据导入数据库
需要使用pandas库和sqlalchemy
python代码:
import pandas as pd from sqlalchemy import create_engine import pymysql pymysql.install_as_MySQLdb() #读取文件数据 data1 = pd.read_excel(r"D:user 1401692Downloads t_flight_execute_flow.xls") conn = create_engine("mysql+pymysql://ares:7i6opxMmkj@ares-m.dbsit.sfcloud.local:3306/ares?charset=utf8") #导入数据库数据 pd.io.sql.to_sql(data1, 'tt_flight_execute_flow_copy0713', conn, schema='ares', if_exists='append',index=False)
以上参考文件路径:
https://blog.csdn.net/weixin_40683253/article/details/86741134