Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 使用Python将数据帧插入SQL server时出错_Sql Server_Python 3.x_Pandas_Dataframe_Sql Insert - Fatal编程技术网

Sql server 使用Python将数据帧插入SQL server时出错

Sql server 使用Python将数据帧插入SQL server时出错,sql-server,python-3.x,pandas,dataframe,sql-insert,Sql Server,Python 3.x,Pandas,Dataframe,Sql Insert,我正在使用Python将取自Excel文件的数据框插入SQL server表中。但是,我得到以下错误: 回溯(最近一次呼叫最后一次): 文件“c:/Python/Task1.py”,第19行,在 cursor.execute(“”) pyodbc.ProgrammingError:('42000','[42000][Microsoft][ODBC SQL Server驱动程序][SQL Server]传入的表格数据流(TDS)远程过程调用(RPC)协议流不正确。参数16(“”):提供的值不是数据

我正在使用Python将取自Excel文件的数据框插入SQL server表中。但是,我得到以下错误:

回溯(最近一次呼叫最后一次): 文件“c:/Python/Task1.py”,第19行,在 cursor.execute(“”) pyodbc.ProgrammingError:('42000','[42000][Microsoft][ODBC SQL Server驱动程序][SQL Server]传入的表格数据流(TDS)远程过程调用(RPC)协议流不正确。参数16(“”):提供的值不是数据类型float的有效实例。请检查源数据中的无效值。无效值的一个示例是刻度大于精度的数字类型的数据。(8023)(SQLExecDirectW)

请参见下面我的语法:

import pyodbc
import pandas as pd

#Import the Excel File into the DataFrame
df = pd.read_excel (r'C:\Users\Diego\Documents\1.Laboral\Jas-Ole\Revenue Management\Reservation_stats_REF.xls')

#Connect Python to SQL Server
conn = pyodbc.connect('Driver={SQL Server};''Server=DIEGO\SQLEXPRESS;''Database=Auxiliary;''Trusted_Connection=yes;')
cursor = conn.cursor()

#Delete a Table data in SQL Server using Python
for row in df.itertuples():
    cursor.execute('''
                DELETE FROM Auxiliary.dbo.Res_statsTEMP
                ''')

#Insert a Table data in SQL Server using Python
for row in df.itertuples():
        cursor.execute('''
                INSERT INTO Auxiliary.dbo.Res_statsTEMP 
                (Confirmation_number,Status,Name,Telephone,Adults,Children,Baby,Start_date,End_date,Stays,
                Reservation_date,Property,Covid_payment,Charged_amount,Revenue,Email,Origin,Market_code,
                Transport) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
                ''',
                row.Confirmation_number,row.Status,row.Name,row.Telephone, row.Adults,row.Children, 
                row.Baby,row.Start_date, row.End_date, row.Stays,row.Reservation_date,row.Property,
                row.Covid_payment,row.Charged_amount, row.Revenue,row.Email,row.Origin,row.Market_code,
                row.Transport)      
conn.commit()
cursor.close()
我认为与dataframe中的数据类型相关的东西与SQL Server表中的数据类型不匹配:

确认号对象 状态对象 名称对象 电话对象 成人int64 儿童int64 婴儿int64 开始日期日期时间64[ns] 结束日期日期时间64[ns] 保持国际64 预订日期时间64[ns] 属性对象 新冠病毒64 押记金额对象 收入浮动64 电子邮件对象 原点对象 市场代码对象 传输对象

不匹配的是说“object”的,但基于Python数据类型,这是SQL中的一个“字符串”,与表中的我的Varchar255匹配

我不知道为什么终端会出现这种错误。我希望您能提供帮助


提前感谢

我建议您将参数作为元组传递

cursor.execute(
            '''
            INSERT INTO Auxiliary.dbo.Res_statsTEMP 
            (Confirmation_number,Status,Name,Telephone,Adults,Children,Baby,Start_date,End_date,Stays,
            Reservation_date,Property,Covid_payment,Charged_amount,Revenue,Email,Origin,Market_code,
            Transport) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
            ''',
            (row.Confirmation_number,row.Status,row.Name,row.Telephone, row.Adults,row.Children, 
            row.Baby,row.Start_date, row.End_date, row.Stays,row.Reservation_date,row.Property,
            row.Covid_payment,row.Charged_amount, row.Revenue,row.Email,row.Origin,row.Market_code,
            row.Transport)
            ) 
并使用将DataFrame columns对象类型更改为字符串类型

df[['Confirmation_number','Status','Name','Telephone','Property','Charged_amount','Email','Origin','Market_code','Transport']].astype(str)