我创建了一个SQL表,其DATETIMES
参数的数据类型为datetime
。我正在从csv文件中读取多行,我正在尝试将日期时间(行[3])从格式%Y%m%d%H%M%S
重新格式化为datetime
兼容%Y-%m-%d %H:%M:%S
。打印d2产生正确的修改日期格式,即2013-12-13 17:32:12,但是使用下面的SQL插入语句触发
Warning: Out of range value for column 'DATETIMES' at row 1
有没有人知道为什么这种类型的日期时间格式可能被拒绝?
for row in csv_data:
if len(row)>=17:
indate = row[3]
d=datetime.strptime(indate, '%Y%m%d%H%M%S')
d2=d.strftime("%Y-%m-%d %H:%M:%S")
parsed = (d2, int(row[2]),int(row[4]),int(row[5]),int(row[6]),int(row[7]),int(row[8]),int(row[9]),int(row[10]),int(row[11]),int(row[12]),int(row[13]),int(row[14]),int(row[15]),int(row[16]))
cursor.execute("""INSERT INTO PR(DATETIMES,PERIOD ,DATA1,DATA2,DATA3, DATA4,DATA5, DATA6, DATA7, DATA8, DATA9, DATA10,DATA11, DAT12, DATA13) \
VALUES("%s", %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) \
ON DUPLICATE KEY UPDATE DATETIMES=values(DATETIMES) ,PERIOD=values(PERIOD),DATA1=values(DATA1) ,DATA2=values(DATA2) ,DATA3=values(DATA3),DATA4=values(DATA4) ,DATA5=values(DATA5) ,DATA6=values(DATA6) ,DATA7=values(DATA7) ,DATA8=values(DATA8) ,DATA9=values(DATA9) ,DATA10=values(DATA10) ,DATA11=values(DATA11) ,DATA12=values(DATA12) ,DATA13=values(DATA13);""", parsed)
mydb.commit()