首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当试图将excel文件导入dataframe时,我的python脚本中靠近'nvarchar‘的SQL语法错误

当试图将excel文件导入dataframe时,我的python脚本中靠近'nvarchar‘的SQL语法错误
EN

Stack Overflow用户
提问于 2022-06-29 16:14:26
回答 1查看 64关注 0票数 0

当前有一个包含我的Server的SQl DOcker映像,我目前正在通过运行一个python脚本连接到它,该脚本将源我的excel文件并将其转换为dataframe,然后该文件将被转换为我的DB的一个表。现在,我一直得到这个错误pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Incorrect syntax near 'nvarchar'. (102) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Statement(s) could not be prepared. (8180)"),我的python代码在这里:

代码语言:javascript
运行
复制
import pandas as pd
import pyodbc

# Import CSV
data = pd.read_excel (r"C:\Users\c.stembridge\OneDrive - NEWREST GROUP SERVICES\Overtime Forecast Report.xlsx", sheet_name='Daily Hrs2')   
df = pd.DataFrame(data)

# Connect to SQL Server
conn = pyodbc.connect("DRIVER={ODBC Driver 18 for SQL Server};SERVER=localhost;UID=SA;PWD=Working@2022;DATABASE=testdb;Encrypt=no;TrustServerCertificate=yes")
cursor = conn.cursor()

# Create Table
cursor.execute('''
        CREATE TABLE Overtime_Forecast (
            date nvarchar(10) primary key,
            day nvarchar(9),
            hrs int,
            dl int,
            catered int,
            hrs_diff_btwn_last_day int,
            catered_flight_diff int,
            employees_OT_count int,
            carriers int,
            whole int,
            half int,
            total_carts int
            )
               ''')

# Insert DataFrame to Table
for row in df.itertuples():
    cursor.execute('''
                INSERT INTO Overtime_Forecast (
                        date nvarchar(10) primary key,
            day nvarchar(9),
            hrs int,
            dl int,
            catered int,
            hrs_diff_btwn_last_day int,
            catered_flight_diff int,
            employees_OT_count int,
            carriers int,
            whole int,
            half int,
            total_carts int)
                VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
                ''',
                row.Date,
                row.Day,
                row.Hrs,
                row.DL,
                row.Catered,
                row.Hrs_diff_btwn_last_day,
                row.Catered_flight_diff,
                row.Employees_OT_count,
                row.Carriers,
                row.Whole,
                row.Half,
                row.Total_carts
                )
conn.commit()
EN

回答 1

Stack Overflow用户

发布于 2022-06-29 16:27:07

从插入中删除类型:

代码语言:javascript
运行
复制
INSERT INTO Overtime_Forecast (
                        date,
            day,
            hrs,
            dl,
            catered,
            hrs_diff_btwn_last_day,
            catered_flight_diff,
            employees_OT_count,
            carriers,
            whole,
            half,
            total_carts ...
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72804705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档