使用cursor.executemany - python将数组插入SQL Server数据库时出错

时间:2017-07-21 13:54:18

标签: python sql sql-server

以下是我的Python代码。我已将输出填充到“Finaldata”中。数组,我试图使用cursor.executemany()插入SQL Server数据库。

但是我得到了以下错误:

 Traceback (most recent call last):
    File "C:/Users/Arvinth Kumar/PycharmProjects/untitled/Country table.py", line 74, in <module>
   cursor.executemany(Insert_SQL,Finaldata)
    File "C:\Users\Arvinth kumar\AppData\Local\Programs\Python\Python35\lib\site-packages\pypyodbc.py", line 1692, in executemany
      self.execute(query_string, params, many_mode = True)
    File "C:\Users\Arvinth Kumar\AppData\Local\Programs\Python\Python35\lib\site-packages\pypyodbc.py", line 1617, in execute
 check_success(self, ret)
    File "C:\Users\Arvinth Kumar\AppData\Local\Programs\Python\Python35\lib\site-packages\pypyodbc.py", line 1007, in check_success
    ctrl_err(SQL_HANDLE_STMT, ODBC_obj.stmt_h, ret, ODBC_obj.ansi)
    File "C:\Users\Arvinth Kumar\AppData\Local\Programs\Python\Python35\lib\site-packages\pypyodbc.py", line 975, in ctrl_err
    raise ProgrammingError(state,err_text)
    pypyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '@P1'.")

       Process finished with exit code 1

源代码:

Finaldata = []
for i in range (120, cnt):
    Finaldata.append ([
        dfc.iloc[i,0],
        dfc.iloc[i,1],
        dfc.iloc[i,2],
        dfc.iloc[i,3],
        dfc.iloc[i,4],
        dfc.iloc[i,5],
        dfc.iloc[i,6],
        dfc.iloc[i,7],
        dfc.iloc[i,8],
        real_earnings[i],
        real_price[i],
        CAPE[i-120],
        percent[i-120]
    ])

print(Finaldata)

Insert_SQL = ("insert into [Tableau].[dbo].[Country_py$] ([Date] ,[Country],[MSCI_Earnings_idx],[MSCI_Price_idx],[Stock_Earnings_idx],[Stock_Price_idx],[10_Yr_Bond_Yld],[Currency_Exchange_Idx],"
                "[CPI], [Real_MSCI_Earnings_idx],[Real_MSCI_Price_idx],[Ten_Yr_CAPE],[Percentile]) values (%?,%?,%?,%?,%?,%?,%?,%?,%?,%?,%?,%?,%?) ")
cursor.executemany(Insert_SQL,Finaldata)

Finaldata数组值:

 [[Timestamp('2005-01-31 00:00:00'), 'SPAIN', '7.65', '105.61', '800.40', 9223.8999999999996, '3.56', '127.61', 86.709999999999994, 9.5662495675239327, 108.43000000000001, 17.014538973937402, 100.0], 
 [Timestamp('2005-02-28 00:00:00'), 'SPAIN', '7.65', '107.27', '795.63', 9391.0, '3.73', '125.83', 86.939999999999998, 9.5409420289855085, 108.43000000000002, 16.896751739115381, 99.333333333333329], 
 [Timestamp('2005-03-31 00:00:00'), 'SPAIN', '7.99', '105.81', '845.28', 9258.7999999999993, '3.64', '128.34', 87.620000000000005, 9.8876477973065509, 108.43000000000001, 16.779617625250101, 98.666666666666671]]

我检查了其他答案,但无法解决我的错误。 谁能帮帮我吗。谢谢!

0 个答案:

没有答案