简体   繁体   English

使用python在现有的SQL Server表中附加来自excel的数据

[英]Appending data from excel in existing SQL Server table using python

I have some CSV files with data which is recurring and therefore I need to update SQL Server by using this python script.我有一些包含重复数据的 CSV 文件,因此我需要使用此 python 脚本更新 SQL Server。

I have tried updating the Microsoft driver for SQL and that doesn't help me.我曾尝试更新 Microsoft 的 SQL 驱动程序,但这对我没有帮助。

Here is my python code :这是我的python代码:

import pandas as pd
import numpy as np
import seaborn as sns
import scipy.stats as stats
import matplotlib.pyplot as plt
from datetime import time 
from datetime import date
import pandas.io.sql
import pyodbc
import xlrd

server ='asd'
db = 'asd'

conn = pyodbc.connect('DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + db + ';UID=asd ;PWD=asd')
cursor=conn.cursor()


query = """
INSERT INTO Db.table (
    Emp_ID  ,
    Global_ID,
    Emp_NAME,
    Org,
    SBU,
    BU,
    Sub_BU,
    HR_Location,
    Swipe_Loc,
    Descp,
    InOutDate,
    InTime,
    OutTime,
    ActHrs,
    ShiftCode,
    AttendanceClassification,
    ActualHrs
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"""


InOutDate= date.today()
InTime = time(11,11,11)
OutTime = time(11,11,11)
ActHrs = time(11,11,11)
ActualHrs = time(11,11,11)

values = ('2134123', '123213', 'Eqqwe', 'Org' , 'SBU' , 'BU ', 'Sub_BU' , 'HR_Location' ,'Swipe_Loc' ,' Descp' , InOutDate , InTime , OutTime , ActHrs , 'ShiftCode' ,'AttendanceClassification' ,ActualHrs )   
cursor.execute(query, values)
conn.close()

Getting the following error when executing query:执行查询时出现以下错误:

Traceback (most recent call last):
File "update.py", line 97, in <module>
cursor.execute(query, values)
pyodbc.Error: ('HYC00', '[HYC00] [Microsoft][ODBC SQL Server Driver]Optional feature not implemented (0) (SQLBindParameter)')

确保日期时间格式在 python 和 SQL 之间兼容

You forgot to add cursor.commit() after execute.您忘记在执行后添加cursor.commit() Execute command can be used only for some selects and read only queries.执行命令只能​​用于某些选择和只读查询。 If you want to change something you shoud add cursor.commit() after.如果您想更改某些内容,您应该在之后添加cursor.commit()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 和 Snowflake 使用 SQL Alchemy 引擎将新数据附加到现有表中返回当前会话没有当前数据库 - Python and Snowflake appending new data into an existing table using SQL Alchemy engine is returning current session does not have a current database 从 SQL Server 获取数据并更新现有 Excel 工作表,同时保持表格可能? - Taking data from SQL Server and updating existing Excel sheet while keeping table possible? 使用python将数据从excel文件导入SQL Server - Importing data from an excel file using python into SQL Server Python SQL Server - 从现有表中的值创建表 - Python SQL Server - Create Table from Values from an existing Table 使用 python 将 SQL 输出数据更新到相应工作表中的现有 Excel - Update SQL output data into Existing Excel in respective sheet using python 使用 python panda 模块附加到 sql 表 - Appending to a sql table using python panda module 在python中使用pyodbc/turbodc从现有表创建SQL表 - create SQL table from existing table using pyodbc/turbodc in python 通过 python 将销售数据作为只有两位小数的 VARCHAR 附加到 MS SQL-Server 表 - Appending sales data to a MS SQL-Server table as a VARCHAR with only two decimal places via python 使用python pyodbc和pandas数据框将数据从SQL服务器导出到Excel - Exporting data from SQL server to Excel using python pyodbc and pandas Data frame Python-将数据从一个xlsx追加到另一个现有的xlsx - Python - Appending data from one xlsx to another existing xlsx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM