[英]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.