我使用sqlalchmey创建一个表:
import numpy as np
import pandas as pd
import time
import pickle
from datetime import datetime
from operator import itemgetter
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy import text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column,Integer,String,Float,TIMESTAMP
Base = declarative_base()
class Ibm_Batch_Tbl_Copy(Base):
__tablename__ = "IBM_BATCH_TBL_COPY"
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=False)
DEL_FLAG = Column(String(1),nullable=True,server_default=text('0'))
REMARK = Column(String(500),nullable=True)
Base.metadata.create_all(engine)
表的创建正常,但是当我向其中插入数据时,它出错了,数据dff
为:
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 day_id 0 2018-10-06 17:51:27.707801 20181006 1 2018-10-06 17:51:27.707801 20181006 2 2018-10-06 17:51:27.707801 20181006 3 2018-10-06 17:51:27.707801 20181006 4 2018-10-06 17:51:27.707801 20181006
然后:
a = []
for i in dff.iterrows():
a.append(dict(i[1]))
engine.execute(Ibm_Batch_Tbl_Copy.__table__.insert(),a)
但是它出错了:IntegrityError: (pymysql.err.IntegrityError) (1062, "Duplicate entry '' for key 'PRIMARY'") [SQL: 'INSERT INTO
IBM_BATCH_TBL_COPY () VALUES ()'] [parameters: ({}, {}, {}, {}, {})] (Background on this error at: http://sqlalche.me/e/gkpj)
。
我不能用三个主键插入数据吗?