我正在编写一个python代码,它将一些非零值从excel表传输到sql数据库表。随函附上密码
data = pd.read_excel('D:\\Python code\\srch_mtx.xlsx', index_col=0)
print(data)
for m in range(len(data)+1):
df_col = list(data.iloc[m][data.iloc[m] > 0].index)
print(df_col)
df_val = list(data.iloc[m][data.iloc[m] > 0])
print(df_val)
# read database
conn = pyodbc.connect('driver={SQL Server}; server=NISHANTDHANORE\\SQLEXPRESS; database=search_matrix;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('SELECT*FROM search_matrix.dbo.blank')
for x, y in enumerate(df_col):
print(y)
print(df_val[x])
query = """UPDATE search_matrix.dbo.blank SET {} = {} WHERE F1='page348'""".format(y, df_val[x])
cursor.execute(query)
conn.commit()
虽然“y”的大多数值是字母字符串,但当“y”的值达到40时,程序抛出如下编程错误
Traceback (most recent call last):
File "D:/SQL db connection/table_fill_v3.py", line 25, in <module>
cursor.execute(query)
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '40'. (102) (SQLExecDirectW)")
有谁能帮我解决这个问题吗?提前谢谢
暂无答案!
目前还没有任何答案,快来回答吧!