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

时间:2018-10-06 04:46:58

标签: python mysql sqlalchemy

我像这样用sqlalchmey创建一个表:

$dir  = './files';
$files1 =  preg_grep('/^([^.])/', scandir($dir));
for($x = 0; $x <= count($files1); $x++){
$f1 = fopen($dir . '/' .$files1[$x], 'r');    //
fseek($f1, 0, SEEK_SET);
while(($line =  fgets($f1)) !== false){
array_push($data_array_list, $line);
}
}

数据:

 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  
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)

尽管它创建了一个表,但是当我向该表中插入数据时,它出错了:a = [] for i in dd.iterrows(): a.append(dict(i[1])) start = time.time() engine.execute(Tbl_Copy1.__table__.insert(),a) end = time.time() TBL_COPY1 InternalError: (pymysql.err.InternalError) (1364, "Field 'DB_CODE' doesn't have a default value") [SQL: 'INSERT INTO,我不知道该如何处理。谁能帮我吗?

0 个答案:

没有答案