internalerror:(pymysql.err.internalerror)在使用sqlalchemy插入数据时

nhn9ugyo  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(257)

我使用sqlalchmey创建一个表,如下所示:

from sqlalchemy import create_engine
from sqlalchemy import text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column,Integer,String,Float,TIMESTAMP
import pandas as pd
import time
from datetime import datetime

Base = declarative_base()
class  Tbl_Copy1(Base):
    __tablename__ = "TBL_COPY1"
    DB_CODE = Column(String(50),primary_key=True)
    DB_OWNER = Column(String(30),primary_key=True)
    TABLE_NAME = Column(String(128),primary_key=True)
    ROW_CNT = Column(Integer,nullable=True)
    TABLE_TIME = Column(Float,nullable=True)
    DAY_ID = Column(String(10),nullable=True)
    BAT_NO = Column(String(20),nullable=True)
    INSERT_TIME = Column(TIMESTAMP(timezone=False),nullable=True)
    DEL_FLAG = Column(String(1),nullable=True,server_default=text('0'))
    REMARK = Column(String(500),nullable=True)
Base.metadata.create_all(engine)

数据:

db_code  db_owner         table_name  row_cnt  table_time   bat_no  \
0   D1022  SOURCING        DIM_COMPANY      0.0    0.157496  D1022_0   
1   D1022  SOURCING   DIM_COMPANY_SITE      0.0    0.157496  D1022_0   
2   D1022  SOURCING       DIM_CONTRACT      0.0    0.157496  D1022_0   
3   D1022  SOURCING    DIM_COST_CENTER      0.0    0.157496  D1022_0   
4   D1022  SOURCING  DIM_ERP_COMMODITY      0.0    0.157496  D1022_0   

           insert_time  
0   2018-10-06 11:52:18.672359  
1   2018-10-06 11:52:18.672359  
2   2018-10-06 11:52:18.672359  
3   2018-10-06 11:52:18.672359  
4   2018-10-06 11:52:18.672359
a = []
for i in dd.iterrows():
    a.append(dict(i[1]))

start = time.time()
engine.execute(Tbl_Copy1.__table__.insert(),a)
end = time.time()

虽然它创建了一个表,但是当我将数据插入这个表时,它出错了: InternalError: (pymysql.err.InternalError) (1364, "Field 'DB_CODE' doesn't have a default value") [SQL: 'INSERT INTO tbl\ U副本1 () VALUES ()'] [parameters: ({}, {}, {}, {}, {}, {}, {}, {} ... displaying 10 of 895203 total bound parameter sets ... {}, {})] (Background on this error at: http://sqlalche.me/e/2j85) ,我不知道该怎么处理。有人能帮我吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题