将熊猫数据框加载到SQL Server 2012

时间:2020-08-15 08:05:32

标签: python sql-server pandas

我尝试将pandas数据框加载到SQL Server。数据框包含以下列:

  • userId-int
  • saleId-int
  • regTime-int
  • countryId-int
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
for index, row in users.iterrows():
     cursor.execute('INSERT INTO [dbo].[users]([userId],[saleId],[regTime],[countryId]) values (?,?,?,?)', 
                     row['userId'], 
                     row['saleId'], 
                     row['regTime'],
                     row['countryId'])
     cnxn.commit()
cursor.close()
cnxn.close()

但是我有以下错误:

---------------------------------------------------------------------------
ProgrammingError                          Traceback (most recent call last)
<ipython-input-103-383c1873cad8> in <module>
      4                     row['saleId'],
      5                     row['regTime'],
----> 6                     row['countryId'])
      7     cnxn.commit()
      8 cursor.close()

ProgrammingError: ('Invalid parameter type.  param-index=0 param-type=numpy.int64', 'HY105')

countryId列的值从0到9(代码)

我该如何纠正?

0 个答案:

没有答案