使用Python插入Teradata时的无效日期

时间:2018-10-08 20:54:09

标签: python sql datetime teradata pyodbc

我正在处理一个python片段,它将使用pyodbc将数据帧插入到teradata表中。我无法逾越的错误是...

File "file.py", line 33, in <module>
cursor.execute("INSERT INTO DB.TABLE (MASDIV,TRXTYPE,STATION,TUNING_EVNT_START_DT,DOW,MOY,TRANSACTIONS)VALUESrow['MASDIV'],'trx_chtr',row['STATION'],row['TUNING_EVNT_START_DT'],row['DOW'],row['MOY'],row['TRANSACTIONS'])
pyodbc.DataError: ('22008', '[22008] [Teradata][ODBC Teradata Driver][TeradataDatabase] Invalid date supplied for Table.TUNING_EVNT_START_DT. (-2666) (SQLExecDirectW)')

为了填补您的麻烦...我有一个Teradata表,我想将一个数据框插入其中。那个桌子是这样的。

CREATE SET TABLE  DB.TABLE, FALLBACK
   (PK decimal(10,0) NOT NULL GENERATED ALWAYS AS IDENTITY
            (START WITH 1 
            INCREMENT BY 1 
            MINVALUE 1 
            --MAXVALUE 2147483647 
            NO CYCLE),
    TRXTYPE VARCHAR(10),
    MASDIV VARCHAR(30),
    STATION VARCHAR(50),
    TUNING_EVNT_START_DT DATE format 'MM/DD/YYYY',
    DOW VARCHAR(3),
    MOY VARCHAR(10),
    TRANSACTIONS INT,
    ANOMALY_FLAG INT NOT NULL DEFAULT 1)
PRIMARY INDEX (PK);

主键和anomaly_flag将自动填写。下面是我正在使用并遇到错误的脚本。它正在读取一个csv并创建一个数据框。 csv的前两行(包括标题)看起来像...

MASDIV              | STATION                    | TUNING_EVNT_START_DT | DOW |    MOY    | TRANSACTIONS

Staten Island       | WFUTDT4                    |         9/12/18      | Wed | September | 538

San Fernando Valley | American Heroes Channel HD |        6/28/2018     | Thu | June      | 12382

这是我正在使用的脚本...

 '''
Written by Bobby October 1st, 2018
REFERENCE
https://tomaztsql.wordpkress.com/2018/07/15/using-python-pandas-dataframe-to-read-and-insert-data-to-microsoft-sql-server/
'''

import pandas as pd
import pyodbc
from datetime import datetime

#READ IN CSV TEST DATA
df = pd.read_csv('Data\\test_set.csv')
print('CSV LOADED')

#ADJUST DATE FORMAT
df['TUNING_EVNT_START_DT'] = pd.to_datetime(df.TUNING_EVNT_START_DT)
#df['TUNING_EVNT_START_DT'] = 
df['TUNING_EVNT_START_DT'].dt.strftime('%m/%d/%Y')
df['TUNING_EVNT_START_DT'] = df['TUNING_EVNT_START_DT'].dt.strftime('%Y-%m-%d')
print('DATE FORMAT CHANGED')
print(df)

#PUSH TO DATABASE
conn = pyodbc.connect('dsn=ConnectR')
cursor = conn.cursor()

# Database table has columns...
# PK | TRXYPE | MASDIV | STATION | TUNING_EVNT_START_DT | DOW | MOY | 
TRANSACTIONS | ANOMALY_FLAG
# PK is autoincrementing, TRXTYPE needs to be specified on insert command, 
and ANOMALY_FLAG defaults to 1 for yes

for index, row in df.iterrows():
        cursor.execute("INSERT INTO DLABBUAnalytics_Lab.Anomaly_Detection_SuperSet(MASDIV,TRXTYPE,STATION,TUNING_EVNT_START_DT,DOW,MOY,TRANSACTIONS)VALUES(?,?,?,?,?,?,?)", row['MASDIV'],'trx_chtr',row['STATION'],row['TUNING_EVNT_START_DT'],row['DOW'],row['MOY'],row['TRANSACTIONS'])
    conn.commit()
    print('RECORD ENTERED')

print('DF SUCCESSFULLY WRITTEN TO DB')

#PULL FROM DATABASE
sql_conn = pyodbc.connect('dsn=ConnectR')
query = 'SELECT * FROM DLABBUAnalytics_Lab.Anomaly_Detection_SuperSet;'
df = pd.read_sql(query, sql_conn)
print(df)

因此,在此我将转换日期格式,并尝试将一行一行地插入Teradata表。第一条记录读入并在数据库中。第二条记录引发在顶部的错误。日期是6/28/18,我将其更改为6/11/18只是为了查看日期和月份是否混淆,但这仍然存在相同的问题。列是否在某处下车,是否正在尝试在date列中插入其他列的值。

任何想法或帮助都将不胜感激!

1 个答案:

答案 0 :(得分:0)

所以问题出在表格的格式。最初,它以CSV格式具有MM / DD / YYYY格式,但是将其更改为YYYY-MM-DD格式可以使脚本完美运行。

谢谢!