0

我有以下数据框

        CALL_DISPOSITION  CITY      END                                     INCIDENT_NUMBER 
0       ADV-Advised       Waterloo  Fri, 23 Mar 2018 01:13:27 GMT           6478983
1       AST-Assist        Waterloo  Sat, 18 Mar 2017 12:41:47 GMT           724030
2       AST-Assist        Waterloo  Sat, 18 Mar 2017 12:41:47 GMT           999000 

我正在尝试将此推送到 IBM DB2 数据库。

我有以下代码:

# IBM DB2 imports
import ibm_db

# instantiate db2 connection
connection_id = ibm_db.connect(
            conn_string,
            '',
            '',
            conn_option,
            ibm_db.QUOTED_LITERAL_REPLACEMENT_OFF)

# create list of tuples from df
records = [tuple(row) for _, row in df.iterrows()]

# Define sql statement structure to replace data into WATERLOO_911_CALLS table
column_names = df.columns
df_sql = "VALUES({}{})".format("?," * (len(column_names) - 1), "?")
sql_command = "REPLACE INTO WATERLOO_911_CALLS {} ".format(df_sql)


# Prepare SQL statement 
try:
    sql_command = ibm_db.prepare(connection, sql_command)
except Exception as e:
     print(e)

# Execute query
try:
    ibm_db.execute_many(sql_command, tuple(temp_records))
except Exception as e:
    print('Data pushing error {}'.format(e))

但是,我不断收到以下错误:

Exception: [IBM][CLI Driver][DB2/LINUXX8664] SQL0104N  An unexpected token "REPLACE INTO WATERLOO_911_CALLS" was found following "BEGIN-OF-STATEMENT".  Expected tokens may include:  "<space>".  SQLSTATE=42601 SQLCODE=-104

我不明白为什么会这样。我遵循了这个repo中列出的步骤,但我似乎无法让它工作。我究竟做错了什么?请让我知道我可以做出任何澄清。

4

1 回答 1

0

它暗示缺少空格,也许它需要VALUE()字符串中的字段之间的空格。喜欢df_sql = "VALUES({}{})".format("?, " * (len(column_names) - 1), "?") 而不是df_sql = "VALUES({}{})".format("?," * (len(column_names) - 1), "?")

只是一种预感。我发现sql_command在执行之前打印也可以帮助排除故障。

于 2020-02-28T20:04:48.240 回答