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



我正在研究一个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 日,只是为了看看是否与日和月混淆,但这仍然存在同样的问题。列是否在某处脱落,并且它正在尝试将不同列的值插入日期列。

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

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

谢谢!

相关内容

  • 没有找到相关文章

最新更新