我想使用sqlalchemy向mysql插入数据,但是出错了,我的代码在下面:
from sqlalchemy import create_engine
import time
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
engine = create_engine("mysql+pymysql://root:password@localhost:3306/test")
Seesion = sessionmaker(bind=engine)
class User(Base):
__tablename__ = 'ssd'
x1 = Column(Integer, primary_key=True)
x2 = Column(String(128))
x3 = Column(String(128))
x4 = Column(Integer)
Base.metadata.create_all(engine)
def test_core(n=2000000):
t0 = time.time()
engine.execute(
User.__table__.insert(),
[{"x1":[i for i in range(n)],'x2':['a'+str(i) for i in range(n)],\
'x3':['bs'+str(i) for i in range(n)],\
'x4':[i for i in range(n,0,-1)]}]
)
print("total time is: ", str(time.time()-t0),'secs')
test_core()
它显示 sqlalchemy.exc.OperationalError:(pymysql.err.OperationalError)(2006,“ MySQL服务器已消失(ConnectionAbortedError(10053,'你的主机中的软件中止了一个已建立的连接。 ',无,10053,无))“)[SQL:'将内插到ssd(x1,x2,x3,x4)值(%(x1)s,%(x2)s,%(x3)s,%(x4) )s)'] [参数:... 。 我该如何处理?该怎么办?