gpt4 book ai didi

python - ('HY000' , 'The SQL contains 21 parameter markers, but 1 parameters were supplied' )

转载 作者:行者123 更新时间:2023-12-04 03:14:05 28 4
gpt4 key购买 nike

我正在尝试读取一个 csv 文件并用 SQL 上传。这是代码。我收到此错误“pypyodbc.ProgrammingError: ('HY000', 'SQL contains 21 parameter markers, but 1 parameters were supplied')” 我的 csv 文件有 21 列。你知道如何解决这个问题吗?

import csv
import pypyodbc
import traceback
import sys
import time
import os.path
import codecs


# Create a connection to DataBase
con = pypyodbc.connect('DRIVER={SQL Server};SERVER=c1devsql01.XXXXXX.com;DATABASE=Parameters;UID=XXXXXX;PWD=XXXX@1')

cur = con.cursor()
query = "insert into Calc_Rules_Metadata_New (Calc_Set, Calc_Set_Identifier, Dependency, Data_Subset_Keys, Calc_Step, Calc_Variable, Calc_Operator, Calc_Operand, By_Variable, Where_Clause, Source_Tracking_Columns, Source_Tracking_Rows, Revision, Tag, Notes, Updated_By, Updated_On, IsDeleted, Metadata_Type, Calculation_Summary) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"

filename = str(sys.argv[1])
basedir = 'C:/RiskClient/InputData/Metadata/Calc'
fullpath = os.path.join(basedir, filename)

with open(fullpath, 'r') as csvfile:
next(csvfile) # Skip Header
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
#for i in range(len(row)):
#if row[i] == '':
# row[i] = None
print(row)
cur.execute(query, row)
cur.commit()

运行时错误:

['1', '1', '1b.B: Question Test 1', '', 'PFA_Unique_Identifier, Fund_Unique_Identifier; Business_Date', '1', 'Total_Borrowing', 'SUM', 'Borrowings_Data.Amount', '', "UPPER(Borrowings_Data.Commitment_Type) IN ('COMMITTED_AND_DRAWN', 'UNCOMMITTED') AND Borrowings_Data.Business_Date = &Rep_Date AND Fund_Unique_Identifier In (select Fund_Unique_Identifier from Fund_Level_Information where Applicable_PF_Sections IS NOT NULL AND PFA_Unique_Identifier = &PFA_UID AND Business_Date = &Rep_Date)", '', '', '', '', '', '', '', '', 'Test Form', '']
Traceback (most recent call last):
File "C:\RiskClient\InputData\Metadata\Calc\CalcMetadata.py", line 47, in <module>
cur.execute(query, row)
File "C:\Program Files\Python3.5.2\lib\site-packages\pypyodbc-1.3.3-py3.5.egg\pypyodbc.py", line 1470, in execute
File "C:\Program Files\Python3.5.2\lib\site-packages\pypyodbc-1.3.3-py3.5.egg\pypyodbc.py", line 1263, in _BindParams
pypyodbc.ProgrammingError: ('HY000', 'The SQL contains 20 parameter markers, but 21 parameters were supplied')

最佳答案

您提供的是单个列表,而不是一整套位置参数。当您在查询字符串中提供这 21 个 ? 而只是 cur.execute(query, row) 中的 row 时,您实际上是在说将该列表填充到第一个 ? 中,并且没有为剩下的二十个提供任何参数。要解决,您需要像这样提供 *args:

cur.execute(query, *row)

确保您的行(即本场景中的列表)实际上有 21 个项目。否则,您仍然会收到有关未提供正确数量参数的错误消息。

关于python - ('HY000' , 'The SQL contains 21 parameter markers, but 1 parameters were supplied' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42543721/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com