首页 > 解决方案 > 将熊猫数据框加载到 SQL Server 2012

问题描述

我尝试将熊猫数据框加载到 SQL Server。数据框具有以下列:

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(代码)

我该如何纠正?

标签: pythonsql-serverpandas

解决方案


推荐阅读